| | |
| | | import com.mindskip.xzs.domain.enums.QuestionTypeEnum; |
| | | import com.mindskip.xzs.domain.question.QuestionItemObject; |
| | | import com.mindskip.xzs.domain.question.QuestionObject; |
| | | import com.mindskip.xzs.excel.CurrencyDataListener; |
| | | import com.mindskip.xzs.excel.FixedMergeCellStrategy; |
| | | import com.mindskip.xzs.excel.SelectExcel; |
| | | import com.mindskip.xzs.excel.*; |
| | | import com.mindskip.xzs.repository.DepartmentMapper; |
| | | import com.mindskip.xzs.repository.SubjectMapper; |
| | | import com.mindskip.xzs.service.*; |
| | |
| | | import com.mindskip.xzs.viewmodel.admin.education.SubjectPageRequestVM; |
| | | import com.mindskip.xzs.viewmodel.admin.question.*; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.mindskip.xzs.vo.QuestionExportVO; |
| | | import com.mindskip.xzs.vo.QuestionImportVO; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.ss.usermodel.DataValidationHelper; |
| | |
| | | import javax.validation.Valid; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | import java.util.function.Consumer; |
| | |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | .sheet("模板") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 4, Arrays.asList(1, 2, 3, 6, 7, 8, 9))) |
| | | .registerWriteHandler(new FixedMergeCellStrategy(2, 4, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | | .doWrite(data); |
| | | } |
| | | |
| | | @GetMapping("/question/export") |
| | | public void importQuestion(QuestionExportVO query, HttpServletResponse response) throws IOException { |
| | | query.formartTime(); |
| | | // 查询导出数据 |
| | | List<QuestionImportVO> exportData = questionService.export(query); |
| | | // 构建数据 |
| | | List<QuestionImportVO> exportList = new ArrayList<>(exportData.size() * 4); |
| | | // 行合并规则 |
| | | List<RowItem> mergeRowList = new ArrayList<>(exportData.size()); |
| | | int j = 2; |
| | | for (QuestionImportVO data : exportData) { |
| | | QuestionObject questionContent = JSON.parseObject(data.getQuestionContent(), QuestionObject.class); |
| | | |
| | | RowItem rowItem = new RowItem(); |
| | | rowItem.setStart(j); |
| | | int end = j + questionContent.getQuestionItemObjects().size() - 1; |
| | | rowItem.setEnd(end); |
| | | mergeRowList.add(rowItem); |
| | | j = end + 1; |
| | | int i = 0; |
| | | for (QuestionItemObject option : questionContent.getQuestionItemObjects()) { |
| | | if (i == 0) { |
| | | QuestionImportVO master = new QuestionImportVO(); |
| | | BeanUtils.copyProperties(data, master); |
| | | if (org.springframework.util.StringUtils.hasText(data.getQuestionType())) { |
| | | master.setQuestionType(QuestionTypeEnum.fromCode(Integer.valueOf(data.getQuestionType())).getName()); |
| | | } |
| | | master.setOptionName(option.getPrefix()); |
| | | master.setOptionValue(option.getContent()); |
| | | master.setTitle(questionContent.getTitleContent()); |
| | | master.setAnalyze(questionContent.getAnalyze()); |
| | | master.setSubjectName(data.getSubjectList().stream().collect(Collectors.joining("、"))); |
| | | master.setCorrect(data.getCorrect().replaceAll(",", "、")); |
| | | BigDecimal score = BigDecimal.valueOf(master.getScore()); |
| | | master.setScore(score.divide(BigDecimal.TEN).intValue()); |
| | | exportList.add(master); |
| | | } else { |
| | | QuestionImportVO optionItem = new QuestionImportVO(); |
| | | optionItem.setOptionName(option.getPrefix()); |
| | | optionItem.setOptionValue(option.getContent()); |
| | | exportList.add(optionItem); |
| | | } |
| | | i++; |
| | | } |
| | | } |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("题目导出数据", "UTF-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); |
| | | |
| | | // 查出所有的课目(excel下拉数据) |
| | | List<Subject> subjects = subjectMapper.allSubject(); |
| | | List<String> subjectNameList = subjects.stream().map(Subject::getName).collect(Collectors.toList()); |
| | | EasyExcel.write(response.getOutputStream(), QuestionImportVO.class) |
| | | .sheet("题目导出数据") |
| | | .registerWriteHandler(new SelectExcel(subjectNameList)) |
| | | .registerWriteHandler(new DynamicMergeCellStrategy(mergeRowList, Arrays.asList(0, 1, 2, 5, 6, 7, 8))) |
| | | .doWrite(exportList); |
| | | } |
| | | |
| | | /** |
| | | * 题目导入 |
| | | * |