| | |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | |
| | | // 构建模板样例数据 |
| | | List<QuestionImportVO> data = new ArrayList<>(8); |
| | | QuestionImportVO questionImportVO = new QuestionImportVO(); |
| | | List<ExamPaperImportVO> data = new ArrayList<>(8); |
| | | ExamPaperImportVO questionImportVO = new ExamPaperImportVO(); |
| | | questionImportVO.setLabel("标题1"); |
| | | questionImportVO.setSubject("语文"); |
| | | questionImportVO.setQuestionType("单选题"); |
| | | questionImportVO.setDifficult(2); |
| | | questionImportVO.setCorrect("A"); |
| | |
| | | questionImportVO.setOptionValue("选我"); |
| | | data.add(questionImportVO); |
| | | |
| | | QuestionImportVO questionImport1 = new QuestionImportVO(); |
| | | ExamPaperImportVO questionImport1 = new ExamPaperImportVO(); |
| | | questionImport1.setOptionName("B"); |
| | | questionImport1.setOptionValue("选B"); |
| | | data.add(questionImport1); |
| | | |
| | | QuestionImportVO questionImport2 = new QuestionImportVO(); |
| | | ExamPaperImportVO questionImport2 = new ExamPaperImportVO(); |
| | | questionImport2.setOptionName("C"); |
| | | questionImport2.setOptionValue("选C"); |
| | | data.add(questionImport2); |
| | | |
| | | QuestionImportVO questionImport3 = new QuestionImportVO(); |
| | | ExamPaperImportVO questionImport3 = new ExamPaperImportVO(); |
| | | questionImport3.setOptionName("D"); |
| | | questionImport3.setOptionValue("选D"); |
| | | data.add(questionImport3); |
| | | |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectService.list(); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), ExamPaperImportVO.class) |
| | | .sheet("模板") |
| | | .registerWriteHandler(new PaperSelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 4, Arrays.asList(2, 3, 6, 7, 8, 9))) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 20, Arrays.asList(0, 1))) |
| | | .doWrite(data); |
| | |
| | | @SneakyThrows |
| | | public void export(QuestionExportVO query, HttpServletResponse response) { |
| | | // 查询导出数据 |
| | | List<QuestionImportVO> exportData = questionService.export(query); |
| | | List<ExamPaperImportVO> exportData = questionService.export(query); |
| | | // 构建数据 |
| | | List<QuestionImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | List<ExamPaperImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | // 行合并规则 |
| | | List<RowItem> mergeRowList = new ArrayList<>(exportData.size()); |
| | | int j = 2; |
| | | for (QuestionImportVO data : exportData) { |
| | | for (ExamPaperImportVO data : exportData) { |
| | | QuestionObject questionContent = JSON.parseObject(data.getQuestionContent(), QuestionObject.class); |
| | | |
| | | RowItem rowItem = new RowItem(); |
| | |
| | | int i = 0; |
| | | for (QuestionItemObject option : questionContent.getQuestionItemObjects()) { |
| | | if (i == 0) { |
| | | QuestionImportVO master = new QuestionImportVO(); |
| | | ExamPaperImportVO master = new ExamPaperImportVO(); |
| | | BeanUtils.copyProperties(data, master); |
| | | if (org.springframework.util.StringUtils.hasText(data.getQuestionType())) { |
| | | master.setQuestionType(QuestionTypeEnum.fromCode(Integer.valueOf(data.getQuestionType())).getName()); |
| | |
| | | master.setScore(score.divide(BigDecimal.TEN).intValue()); |
| | | exportList.add(master); |
| | | } else { |
| | | QuestionImportVO optionItem = new QuestionImportVO(); |
| | | ExamPaperImportVO optionItem = new ExamPaperImportVO(); |
| | | optionItem.setOptionName(option.getPrefix()); |
| | | optionItem.setOptionValue(option.getContent()); |
| | | exportList.add(optionItem); |
| | |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectService.list(); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | EasyExcel.write(response.getOutputStream(), ExamPaperImportVO.class) |
| | | .sheet("题目导出数据") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new DynamicMergeCellStrategy(mergeRowList, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | |
| | | // 一张试卷多个标题 |
| | | ArrayList<PaperFixQuestionDTO> list = new ArrayList<>(); |
| | | |
| | | Consumer<List<QuestionImportVO>> consumer = (data) -> { |
| | | Consumer<List<ExamPaperImportVO>> consumer = (data) -> { |
| | | // 循环每一行 |
| | | for (int i = 0; i < data.size(); i++) { |
| | | // 读取的题目 |
| | | QuestionImportVO excelQuestion = data.get(i); |
| | | ExamPaperImportVO excelQuestion = data.get(i); |
| | | String questionType = excelQuestion.getQuestionType(); |
| | | String label = excelQuestion.getLabel(); |
| | | // 判断是否标题 |
| | |
| | | if (i + 1 == data.size()) { |
| | | break; |
| | | } |
| | | QuestionImportVO nextQuestion = data.get(1 + i); |
| | | ExamPaperImportVO nextQuestion = data.get(1 + i); |
| | | if (nextQuestion.intact()) { |
| | | break; |
| | | } |
| | |
| | | question.setDeleted(0); |
| | | question.setQuestionType(QuestionTypeEnum.get(questionType)); |
| | | // 根据科目名称获取id |
| | | QuestionImportVO finalExcelQuestion = excelQuestion; |
| | | ExamPaperImportVO finalExcelQuestion = excelQuestion; |
| | | question.setSubjectId(subjects.stream().filter(subject -> subject.getName().equals(finalExcelQuestion.getSubject())).findFirst().get().getId()); |
| | | questions.add(question); |
| | | |
| | |
| | | paperQuestion.setScore(BigDecimal.valueOf(excelQuestion.getScore())); |
| | | paperQuestions.add(paperQuestion); |
| | | } |
| | | // 下一条数据是标题 |
| | | if (i + 1 == data.size() || data.get(i + 1).master()) { |
| | | break; |
| | | } |
| | |
| | | } |
| | | } |
| | | }; |
| | | EasyExcel.read(file.getInputStream(), QuestionImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | EasyExcel.read(file.getInputStream(), ExamPaperImportVO.class, new CurrencyDataListener(consumer)).sheet("模板").doRead(); |
| | | // 保存题目 |
| | | questionService.saveBatch(questions); |
| | | // 保存试卷 |