1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>(); colList.add(new ExcelExportEntity("第一列", "first")); colList.add(new ExcelExportEntity("第二列", "second")); colList.add(new ExcelExportEntity("第三列", "third"));
colList.stream().forEach(excelExportEntity -> excelExportEntity.setNeedMerge(true));
ExcelExportEntity detailGroup = new ExcelExportEntity("明细", "detail"); List<ExcelExportEntity> detailGroupList = new ArrayList<ExcelExportEntity>(); detailGroupList.add(new ExcelExportEntity("明细第一列", "detailFirst")); detailGroupList.add(new ExcelExportEntity("明细第二列", "detailSecond")); detailGroupList.add(new ExcelExportEntity("明细第三列", "detailThird")); detailGroup.setList(detailGroupList); colList.add(detailGroup);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
List<MainDto> mainDtos = mapper.selectAll(mainVm);
for (MainDto mainDto:mainDtos) {
Map<String, Object> valMap = new HashMap<String, Object>(); valMap.put("first", mainDto.getFirst()); valMap.put("second", mainDto.getSecond()); valMap.put("third", mainDto.getThird());
List<DetailDto> detailDtos = detailMapper.selectByMainId(mainDto.getMainId());
List<Map<String, Object>> deliList = new ArrayList<Map<String, Object>>(); for (DetailDto detailDto:detailDtos) { Map<String, Object> deliValMap = new HashMap<String, Object>(); deliValMap.put("detailFirst", detailDto.getDetailFirst()); deliValMap.put("detailSecond", detailDto.getDetailSecond()); deliValMap.put("detailThird", detailDto.getDetailThird()); deliList.add(deliValMap); } valMap.put("detail", deliList); list.add(valMap); }
try { httpServletResponse.setHeader("content-Type", "application/vnd.ms-excel;charset=utf-8"); httpServletResponse.setContentType("application/vnd.ms-excel"); httpServletResponse.setCharacterEncoding("UTF-8"); httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("XX表.xlsx", "utf-8")); ExportParams exportParams = new ExportParams(); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, colList, list); workbook.write(httpServletResponse.getOutputStream());
} catch (Exception e) { log.error(e.getMessage(), e); throw new CloudmesException("文件导出错误,请重试"); }
|