springboot集成easypoi实现excel多sheet导入案例

springboot集成easypoi实现excel多sheet导入案例

  1. pom依赖

    <!--easypoi依赖,excel导入导出-->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-spring-boot-starter</artifactId>
        <version>4.3.0</version>
    </dependency>
    
  2. 核心工具类

    package com.example.demo.util;
    import cn.afterturn.easypoi.excel.ExcelImportUtil;
    import cn.afterturn.easypoi.excel.entity.ImportParams;
    import java.io.InputStream;import java.util.List;
    import java.util.NoSuchElementException;
    /**
     * @author luoYong
     * @version 1.0
     * @date 2022/2/21 10:40
     */
    public class EasyPoiUtils {
        /**
         * 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合
         *
         * @param inputStream  excel输入流
         * @param sheetIndex 导入sheet索引
         * @param titleRows  表标题的行数
         * @param headerRows 表头行数
         * @param pojoClass  Excel实体类
         */
        public static <T> List<T> importExcel(InputStream inputStream, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
            // 根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页
            ImportParams params = new ImportParams();
            // 第几个sheet表页
            params.setStartSheetIndex(sheetIndex);
            //设置表标题行数
            params.setTitleRows(titleRows);
            //设置表头行数
            params.setHeadRows(headerRows);
            List<T> list = null;
            try {
                //读取的excel数据集合
                list = ExcelImportUtil.importExcel(inputStream, pojoClass, params);
            } catch (NoSuchElementException e) {
                throw new RuntimeException("模板不能为空");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }
    }
    
  3. 导入实体类

    import cn.afterturn.easypoi.excel.annotation.Excel;
    import lombok.Data;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @Author: Administrator
     * @Date: 2022/09/16/12:16
     * @Description:单选题导入
     */
    @Data
    public class SheetRadioExcel {
    
        @Excel(name = "试题内容",width = 50, isImportField = "true_st")
        private String content;
    
        @Excel(name = "正确选项内容",width = 25,isImportField = "true_st")
        private String rightContent;
    
        @Excel(name = "选项A",width = 50, isImportField = "true_st")
        private String optionA;
    
        @Excel(name = "选项B",width = 50, isImportField = "true_st")
        private String optionB;
    
        @Excel(name = "选项C",width = 50, isImportField = "true_st")
        private String optionC;
    
        @Excel(name = "选项D",width = 50, isImportField = "true_st")
        private String optionD;
    }
    	
    //多选题导入
    import cn.afterturn.easypoi.excel.annotation.Excel;
    import lombok.Data;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @Author: Administrator
     * @Date: 2022/09/16/12:20
     * @Description:
     */
    @Data
    public class SheetMultipleExcel {
    
        @Excel(name = "试题内容",width = 50, isImportField = "true_st")
        private String content;
    
        @Excel(name = "正确选项内容(多个选项之间用'*'隔开)",width = 25,isImportField = "true_st")
        private String rightContent;
    
        @Excel(name = "选项A",width = 50, isImportField = "true_st")
        private String optionA;
    
        @Excel(name = "选项B",width = 50, isImportField = "true_st")
        private String optionB;
    
        @Excel(name = "选项C",width = 50, isImportField = "true_st")
        private String optionC;
    
        @Excel(name = "选项D",width = 50, isImportField = "true_st")
        private String optionD;
    }
    // 判断题导入
    import cn.afterturn.easypoi.excel.annotation.Excel;
    import lombok.Data;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @Author: Administrator
     * @Date: 2022/09/16/12:25
     * @Description:
     */
    @Data
    public class SheetJudgeExcel {
    
        @Excel(name = "试题内容",width = 50, isImportField = "true_st")
        private String content;
    
        @Excel(name = "正确内容",width = 25,isImportField = "true_st")
        private String rightContent;
    
        @Excel(name = "选项1",width = 50, isImportField = "true_st")
        private String judgeA;
    
        @Excel(name = "选项2",width = 50, isImportField = "true_st")
        private String judgeB;
    }
    
  4. 导入方法

    public R importFile(@RequestParam("file") MultipartFile file, @RequestParam("panoramaId") Long panoramaId) {
            if (file.isEmpty()) {
                return R.error("文件不能为空");
            }
            // 单选题
            List<SheetRadioExcel> sheetRadioList = null;
            // 多选题
            List<SheetMultipleExcel> sheetMultipleList = null;
            // 判断题
            List<SheetJudgeExcel> sheetJudgeList = null;
    
            //分批进行读取excel的sheet工作表
            //读取第一个sheet表
            try {
                sheetRadioList = EasyPoiUtils.importExcel(file.getInputStream(), 0, 1, 1, SheetRadioExcel.class);
                //读取第二个sheet表
                sheetMultipleList = EasyPoiUtils.importExcel(file.getInputStream(), 1, 1, 1, SheetMultipleExcel.class);
                // 读取第三个sheet表
                sheetJudgeList = EasyPoiUtils.importExcel(file.getInputStream(), 2, 1, 1, SheetJudgeExcel.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                // 单选题
                if (sheetRadioList != null && sheetRadioList.size() > 0) {
                    for (SheetRadioExcel radioQuestion:sheetRadioList) {
                        if (StringUtils.isBlank(radioQuestion.getContent()) || StringUtils.isBlank(radioQuestion.getRightContent())) {
                            return R.error("内容、正确答案内容不能为空");
                        }
                        String[] radioRightContents = radioQuestion.getRightContent().split("\\*");
                        if (radioRightContents.length > 1) {
                            return R.error("单选题只能设置一个正确选项");
                        }
                        // 设置单选题答案选项
                        radioQuestion(radioQuestion, panoramaId);
                    }
                }
                // 多选题
                if (sheetMultipleList != null && sheetMultipleList.size() > 0) {
                    for (SheetMultipleExcel mutipleQuestion:sheetMultipleList) {
                        if (StringUtils.isBlank(mutipleQuestion.getContent()) || StringUtils.isBlank(mutipleQuestion.getRightContent())) {
                            return R.error("内容、正确答案内容不能为空");
                        }
                        multipleQuestion(mutipleQuestion, panoramaId);
                    }
                }
                // 判断题
                if (sheetJudgeList != null && sheetJudgeList.size() > 0) {
                    for (SheetJudgeExcel judgeQuestion:sheetJudgeList) {
                        if (StringUtils.isBlank(judgeQuestion.getContent()) || StringUtils.isBlank(judgeQuestion.getRightContent())) {
                            return R.error("内容、正确答案内容不能为空");
                        }
                        String[] radioRightContents = judgeQuestion.getRightContent().split("\\*");
                        if (radioRightContents.length > 1) {
                            return R.error("判断题只能设置一个正确选项");
                        }
                        judgeQuestion(judgeQuestion, panoramaId);
                    }
                }
            }catch (Exception e) {
                return R.error("试题导入失败");
            }
            return R.ok();
        }
    	private void radioQuestion(SheetRadioExcel radioQuestion, Long panoramaId){
        List<QuestionAnswerEntity> questionAnswerList = new ArrayList<>();
        QuestionForm form = new QuestionForm();
        form.setContent(radioQuestion.getContent());
        form.setLevel(1);
        form.setQuType(Constant.RADIO);
        form.setPanoramaId(panoramaId);
        QuestionAnswerEntity radioAnswerA = new QuestionAnswerEntity();
        radioAnswerA.setContent(radioQuestion.getOptionA());
        if (radioQuestion.getRightContent().equals(radioQuestion.getOptionA())) {
            radioAnswerA.setIsRight(true);
        } else {
            radioAnswerA.setIsRight(false);
        }
        questionAnswerList.add(radioAnswerA);
        QuestionAnswerEntity radioAnswerB = new QuestionAnswerEntity();
        radioAnswerB.setContent(radioQuestion.getOptionB());
        if (radioQuestion.getRightContent().equals(radioQuestion.getOptionB())) {
            radioAnswerB.setIsRight(true);
        } else {
            radioAnswerB.setIsRight(false);
        }
        questionAnswerList.add(radioAnswerB);
        QuestionAnswerEntity radioAnswerC = new QuestionAnswerEntity();
        radioAnswerC.setContent(radioQuestion.getOptionC());
        if (radioQuestion.getRightContent().equals(radioQuestion.getOptionC())) {
            radioAnswerC.setIsRight(true);
        } else {
            radioAnswerC.setIsRight(false);
        }
        questionAnswerList.add(radioAnswerC);
        QuestionAnswerEntity radioAnswerD = new QuestionAnswerEntity();
        radioAnswerD.setContent(radioQuestion.getOptionD());
        if (radioQuestion.getRightContent().equals(radioQuestion.getOptionD())) {
            radioAnswerD.setIsRight(true);
        } else {
            radioAnswerD.setIsRight(false);
        }
        questionAnswerList.add(radioAnswerD);
        form.setAnswerList(questionAnswerList);
        questionService.saveQuestion(form);
    }
    
    public void multipleQuestion(SheetMultipleExcel mutipleQuestion, Long panoramaId) {
        List<QuestionAnswerEntity> multipleAnswerList = new ArrayList<>();
        QuestionForm form = new QuestionForm();
        form.setContent(mutipleQuestion.getContent());
        form.setLevel(1);
        form.setQuType(Constant.MULTIPLE);
        form.setPanoramaId(panoramaId);
        // 选项内容
        String[] optionContent = mutipleQuestion.getRightContent().split("\\*");
        QuestionAnswerEntity multipleOptionAnswerA = new QuestionAnswerEntity();
    
        multipleOptionAnswerA.setContent(mutipleQuestion.getOptionA());
        a:for (String rightContent:optionContent) {
            if (rightContent.equals(mutipleQuestion.getOptionA())) {
                multipleOptionAnswerA.setIsRight(true);
                break a;
            } else {
                multipleOptionAnswerA.setIsRight(false);
            }
        }
        multipleAnswerList.add(multipleOptionAnswerA);
    
        QuestionAnswerEntity multipleOptionAnswerB = new QuestionAnswerEntity();
        multipleOptionAnswerB.setContent(mutipleQuestion.getOptionB());
        a: for (String rightContent:optionContent) {
            if (rightContent.equals(mutipleQuestion.getOptionB())) {
                multipleOptionAnswerB.setIsRight(true);
                break a;
            } else {
                multipleOptionAnswerB.setIsRight(false);
            }
        }
        multipleAnswerList.add(multipleOptionAnswerB);
    
        QuestionAnswerEntity multipleOptionAnswerC = new QuestionAnswerEntity();
        multipleOptionAnswerC.setContent(mutipleQuestion.getOptionC());
        a: for (String rightContent:optionContent) {
            if (rightContent.equals(mutipleQuestion.getOptionC())) {
                multipleOptionAnswerC.setIsRight(true);
                break a;
            } else {
                multipleOptionAnswerC.setIsRight(false);
            }
        }
        multipleAnswerList.add(multipleOptionAnswerC);
    
        QuestionAnswerEntity multipleOptionAnswerD = new QuestionAnswerEntity();
        multipleOptionAnswerD.setContent(mutipleQuestion.getOptionD());
        a: for (String rightContent:optionContent) {
            if (rightContent.equals(mutipleQuestion.getOptionD())) {
                multipleOptionAnswerD.setIsRight(true);
                break a;
            } else {
                multipleOptionAnswerD.setIsRight(false);
            }
        }
        multipleAnswerList.add(multipleOptionAnswerD);
        form.setAnswerList(multipleAnswerList);
        questionService.saveQuestion(form);
    }
    
    public void judgeQuestion(SheetJudgeExcel judgeQuestion, Long panoramaId) {
        QuestionForm form = new QuestionForm();
        form.setContent(judgeQuestion.getContent());
        form.setLevel(1);
        form.setQuType(Constant.JUDGE);
        form.setPanoramaId(panoramaId);
        List<QuestionAnswerEntity> judgeQuestionAnswerList = new ArrayList<>();
        QuestionAnswerEntity judgeOptionA = new QuestionAnswerEntity();
        judgeOptionA.setContent(judgeQuestion.getJudgeA());
        if (judgeQuestion.getJudgeA().equals(judgeQuestion.getRightContent())) {
            judgeOptionA.setIsRight(true);
        } else {
            judgeOptionA.setIsRight(false);
        }
        judgeQuestionAnswerList.add(judgeOptionA);
        QuestionAnswerEntity judgeOptionB = new QuestionAnswerEntity();
        judgeOptionB.setContent(judgeQuestion.getJudgeB());
        if (judgeQuestion.getJudgeB().equals(judgeQuestion.getRightContent())) {
            judgeOptionB.setIsRight(true);
        } else  {
            judgeOptionB.setIsRight(false);
        }
        judgeQuestionAnswerList.add(judgeOptionB);
        form.setAnswerList(judgeQuestionAnswerList);
        questionService.saveQuestion(form);
    }
    
  5. 实现类添加数据

    /**
     * 保存问题
     * @param form
     */
    void saveQuestion(QuestionForm form);
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveQuestion(QuestionForm form) {
        QuestionEntity question = new QuestionEntity();
        question.setAnalysis(form.getAnalysis());
        question.setContent(form.getContent());
        question.setCreateTime(new Date());
        question.setLevel(form.getLevel());
        question.setQuType(form.getQuType());
        question.setRemark(form.getRemark());
        this.save(question);
        // 下面两个是试题关系表
        questionAnswerService.saveAll(question.getId(),form.getAnswerList());
        // 保存到题库
        panoramaQuestionService.saveAll(question.getId(), question.getQuType(), form.getPanoramaId());
    
    }
    
  6. 效果
    springboot集成easypoi实现excel多sheet导入案例_第1张图片
    springboot集成easypoi实现excel多sheet导入案例_第2张图片

你可能感兴趣的:(springboot,Java,spring,boot,easypoi多sheet导入)