这块是导出,并不需要实体类中的类型上的注解的添加
easyExcel并不能实现实体类中有实体类的属性或者是LIST类型的属性
因此使用EASYPOI
步骤二:重写poi的导出方法 /** * 导出Ecel * * @return org.apache.poi.ss.usermodel.Workbook * @author zhuyongsheng * @date 2023/11/6 */ private static Workbook exportExcel(List list, ExcelType type) { Workbook workbook = new HSSFWorkbook(); for (Map map : list) { MyExcelExportService service = new MyExcelExportService(); service.createSheetWithList(workbook, (ExportParams) map.get("title"), ExportParams.class, (List) map.get("entityList"), (Collection) map.get("data")); } return workbook; } 步骤三:重写校验将Data中的值给Sheet的服务MyExcelExportService
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;import cn.afterturn.easypoi.excel.entity.ExportParams;import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;import cn.afterturn.easypoi.excel.export.ExcelExportService;import cn.afterturn.easypoi.exception.excel.ExcelExportException;import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum;import cn.afterturn.easypoi.util.PoiPublicUtil;import lombok.extern.slf4j.Slf4j;import org.apache.poi.ss.usermodel.Workbook;import java.lang.reflect.Field;import java.util.Collection;import java.util.List;/** * @program: pc * @description * @author: yangtao * @create: 2023-06-29 13:17 **/@Slf4jpublic class MyExcelExportService extends ExcelExportService { public void createSheetWithList(Workbook workbook, ExportParams entity, Class pojoClass, List entityList, Collection dataSet) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Excel export start ,class is {}", pojoClass); LOGGER.debug("Excel version is {}", entity.getType().equals(ExcelType.HSSF) ? "03" : "07"); } if (workbook == null || entity == null || pojoClass == null || dataSet == null) { throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); } try { List excelParams = entityList; // 得到所有字段 Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); String targetId = etarget == null ? null : etarget.value(); getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, null, null); //获取所有参数后,后面的逻辑判断就一致了 createSheetForMap(workbook, entity, excelParams, dataSet); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); } }} 步骤四:定义列头(包括动态列头) /** * 定义表格样式 * * @return java.util.List * @since 2.8.2 */ private List setExportExcelStyle(EvaluationRecordVo evaluationRecordVo) { System.out.println("开始创建模板-----"); //定义表格列名,该集合存放的就是表格的列明,每个对象就是表格中的一列 List modelList = new ArrayList(); //该对象就是定义列属性的对象 ExcelExportEntity excelentity = null; excelentity = new ExcelExportEntity("学生姓名", "peopleName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("年龄", "age"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("性别", "sexName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("院系", "studentDepartmentName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("专业", "studentMajorName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("班级", "studentClassName"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("测评总分", "score"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("测评结果", "evaluationResultsPageDisplay"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity); excelentity = new ExcelExportEntity("题号/题目名称", "srco"); excelentity.setWidth(20); excelentity.setHeight(10); modelList.add(excelentity);// ---------------------定义自定义动态列头 //量表的所有的题目以及内容 List titleAndContentVos = papersExaminationResultAnswerInfoMapper.titleAndContent(evaluationRecordVo.getPaperId()); for (TitleAndContentVo titleAndContentVo : titleAndContentVos) { ExcelExportEntity excelExportEntity = new ExcelExportEntity(); excelExportEntity.setName(titleAndContentVo.getPaperNumber() + "." + titleAndContentVo.getQuestionsContent()); excelExportEntity.setKey(titleAndContentVo.getQuestionsId() + "APO"); excelExportEntity.setHeight(15); modelList.add(excelExportEntity); } System.out.println("创建模板结束-----"); return modelList; } 步骤五:定义数据源 /** * 定义数据 * * @return */ private List getData(EvaluationRecordVo evaluationRecordVo) { System.out.println("开始获取数据源结束-----"); //获取数据源 List dataList = new ArrayList(); //存储表格中的每一行数据 Map mapParent = null; //获取该测评下的学生的基本信息以及测评结果和分数 List assessmentResultsAndScoresVos = papersExaminationResultAnswerInfoMapper.getDataOneToMany(evaluationRecordVo.getExaminationId(), evaluationRecordVo.getPaperId(), evaluationRecordVo.getOrganizationId()); System.out.println("总计人数:" + assessmentResultsAndScoresVos.size()); for (AssessmentResultsAndScoresVo assessmentResultsAndScoresVo : assessmentResultsAndScoresVos) {//10849 mapParent = new HashMap(); mapParent.put("peopleName", assessmentResultsAndScoresVo.getPeopleName()); mapParent.put("age", assessmentResultsAndScoresVo.getAge()); mapParent.put("sexName", assessmentResultsAndScoresVo.getSexName()); mapParent.put("studentDepartmentName", assessmentResultsAndScoresVo.getStudentDepartmentName()); mapParent.put("studentMajorName", assessmentResultsAndScoresVo.getStudentMajorName()); mapParent.put("studentClassName", assessmentResultsAndScoresVo.getStudentClassName()); mapParent.put("score", assessmentResultsAndScoresVo.getScore()); mapParent.put("evaluationResultsPageDisplay", assessmentResultsAndScoresVo.getEvaluationResultsPageDisplay()); mapParent.put("srco", assessmentResultsAndScoresVo.getSrco()); for (TheStudentScoreVo theStudentScoreVo : assessmentResultsAndScoresVo.getTheStudentScoreVoList()) { String[] strs = theStudentScoreVo.getRepose().split("-"); mapParent.put(strs[0] + "APO", strs[1]); } dataList.add(mapParent); System.out.println("获取数据源结束-----"); } int size = assessmentResultsAndScoresVos.size(); return dataList; }**这块的内容自己获取 保证返回的格式就OK了 **
步骤六:导出这块主要是将列头和数据源给到Excel对象进行创建Excel文件和格式然后导出
/** * 报表导出 * * @param response response * @return javax.servlet.http.HttpServletResponse * @author huan * @date 2023/6/21 * @since 2.8.2 */ @Override public HttpServletResponse export(HttpServletResponse response, EvaluationRecordVo evaluationRecordVo) { System.out.println("数据导出中----"); //定义表格样式 之前的列头的定义的东西 List modelList = setExportExcelStyle(evaluationRecordVo); //定义表格名称 之前的获取的数据 try { String fileName = URLEncoder.encode("StudentAnswerOptions", "utf-8"); List data = getData(evaluationRecordVo); int size = data.size(); // 将sheet1、sheet2使用得map进行包装 List sheetsList = new ArrayList(); //如果需要是多个的话就创建多个的ExportParams对象放到sheetsList集合中就ok了 // Sheet样式 ExportParams sheetExportParams = new ExportParams(); // 设置sheet得名称 sheetExportParams.setSheetName("学生选项详情"); sheetExportParams.setTitleHeight((short) 20); // 创建sheet使用得map Map sheet1ExportMap = new HashMap(); // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName sheet1ExportMap.put("title", sheetExportParams); //sheet1样式 sheet1ExportMap.put("entityList", modelList); //sheet1中要填充得数据,true表示查询入库数据,false表示查询易签待入库数据 sheet1ExportMap.put("data", data); sheetsList.add(sheet1ExportMap); // 执行方法 Workbook workBook = exportExcel(sheetsList, ExcelType.XSSF); //设置response response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xls"); //设置编码格式 response.setCharacterEncoding("GBK"); //将表格内容写到输出流中并刷新缓存 @Cleanup ServletOutputStream out = null; out = response.getOutputStream(); workBook.write(out); out.flush(); workBook.close(); } catch (Exception e) { e.printStackTrace(); } return response; } 前端对接进行Excel下载 let blob = new Blob([res], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型application/soap+xml; charset=utf-8 let downloadElement = document.createElement("a"); let href = window.URL.createObjectURL(blob); // 创建下载的