hutools实现excel导入

首先引入hutool-all包和apache.poi包 且poi包需要大于4.1.2

   <!--        hutools-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.8</version>
        </dependency>

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

核心方法

/**
     * 文件名
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload/excel")
    @CrossOrigin
    public ApiResult uploadLocal(@RequestPart(value = "file")  MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            ApiResultHandler.buildApiResult(400, "文件不能为空", null);
        }
        String orgName = file.getOriginalFilename();// 获取文件名
        orgName = this.getFileName(orgName);
        System.out.println("文件名"+orgName);
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
//            读取表头 对应实体内部的属性名
            reader.addHeaderAlias("问题", "question");
            reader.addHeaderAlias("所属科目", "subject");
            reader.addHeaderAlias("难度", "level");
            reader.addHeaderAlias("选项A", "answerA");
            reader.addHeaderAlias("选项B", "answerB");
            reader.addHeaderAlias("选项C", "answerC");
            reader.addHeaderAlias("选项D", "answerD");
            reader.addHeaderAlias("正确答案", "rightAnswer");
            reader.addHeaderAlias("题目解析", "analysis");
            reader.addHeaderAlias("分数", "score");
            List<MultiQuestion> list =  reader.readAll(MultiQuestion.class);
            AtomicInteger i = new AtomicInteger();
            list.forEach(item->{
                MultiQuestion multiQuestion = new MultiQuestion();
                BeanUtils.copyProperties(item,multiQuestion);
//                执行插入方法
                int add = multiQuestionService.add(multiQuestion);
                if(add>0){
                    i.getAndIncrement();
                }
            });
            return ApiResultHandler.buildApiResult(200, "导入成功,共计导入["+i+"]条数据", null);
        } catch (Exception e) {
            return ApiResultHandler.buildApiResult(400, "导入失败", null);
        }
    }

对应实体类

package com.exam.entity;

import lombok.Data;

// 选择题实体
@Data
public class MultiQuestion {
    //    问题id
    private Integer questionId;
    //问题
    private String question;
    //    所属科目
    private String subject;

    //
    private String section;
    // 难度 4为易错题
    private String level;
    //    选项a
    private String answerA;
    //选项b
    private String answerB;
    //选项c
    private String answerC;
    // 选项d
    private String answerD;


    // 正确答案
    private String rightAnswer;

    private String analysis; //题目解析

    private String imageUrl; //题目图片
    //  分数
    private Integer score;

}

示例excel

问题所属科目难度选项A选项B选项C选项D正确答案题目解析分数
test1科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2
test2科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2
test3科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2
test4科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2
test5科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2
test6科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2
test7科目一模拟4这个是正确的这个是错误的这个也是错误的这个还是错误的A测试数据2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飓廴之莨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值