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 62 63 64 65
| @PostMapping("/excel/export") @ApiOperation(value = "通用导出") @LogAnnotation(module = IasrConstants.MODULE, recordRequestParam = true) public void exportQueryData(@RequestBody ExcelQueryDto excelQueryDto, HttpServletResponse response) { long startTime = System.currentTimeMillis(); if(ObjectUtils.isEmpty(excelQueryDto) || StringUtils.isEmpty(excelQueryDto.getQuerySql())){ throw new BusinessException("无导出sql,请检查!"); } ExcelWriter excelWriter = null; try { Pageable pageable = PageRequest.of(0, Integer.parseInt(exportDataNums)); Page<Map<String, Object>> dataPage = projPlanReportRepository.findReportPage(pageable, excelQueryDto.getQuerySql(), excelQueryDto.getParamList()); String fileName = excelQueryDto.getFileName(); fileName = Optional.ofNullable(fileName).orElse("导出数据"); fileName = URLEncoder.encode(fileName, "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
String dataSheetName = StringUtils.isEmpty(excelQueryDto.getDataSheetName()) ? "查询数据" : excelQueryDto.getDataSheetName(); WriteSheet writeSheet = EasyExcel.writerSheet(dataSheetName).build(); excelWriter = EasyExcel.write(response.getOutputStream()).build(); if(!ObjectUtils.isEmpty(dataPage) && !CollectionUtils.isEmpty(dataPage.getContent())){ List<String> excelTitleList = CollectionUtils.isEmpty(excelQueryDto.getExcelTitleList())?new ArrayList<>(dataPage.getContent().get(0).keySet()):excelQueryDto.getExcelTitleList(); List<List<String>> headerList = Collections.singletonList(excelTitleList); excelWriter.write(headerList, writeSheet); int totalPage = dataPage.getTotalElements()>100000?100000/Integer.parseInt(exportDataNums):dataPage.getTotalPages(); int currPage = 0; logger.info("查询数据共"+totalPage+"页,数据共"+dataPage.getTotalElements()+"条。。。"); while (currPage<=totalPage){ logger.info("开始查询第"+currPage+"页。。。。"); try { pageable = PageRequest.of(currPage, Integer.parseInt(exportDataNums)); Page<Map<String, Object>> pageData = projPlanReportRepository.findReportPage(pageable, excelQueryDto.getQuerySql(), excelQueryDto.getParamList()); if (!ObjectUtils.isEmpty(pageData) && !CollectionUtils.isEmpty(pageData.getContent())) { Map<String, Map<String, Object>> dictionaryMap = excelQueryDto.getDictitoryMap(); List<List<Object>> dataList = getExcelDateList(excelQueryDto, pageData, headerList, dictionaryMap); excelWriter.write(dataList, writeSheet); } } catch (Exception exception) { logger.error("查询异常", exception); } logger.info("查询第"+currPage+"页结束"); currPage++; } if(!ObjectUtils.isEmpty(excelQueryDto.getExportSqlFlag()) && excelQueryDto.getExportSqlFlag()){ String sqlSheetName = StringUtils.isEmpty(excelQueryDto.getSqlSheetName()) ? "查询sql" : excelQueryDto.getSqlSheetName(); WriteSheet writeSqlSheet = EasyExcel.writerSheet(2,sqlSheetName).build(); List<List<String>> dataList = Collections.singletonList(Collections.singletonList(excelQueryDto.getQuerySql())); excelWriter.write(dataList,writeSqlSheet); } excelWriter.finish(); } } catch (Exception e) { logger.error("导出excel异常", e); throw new BusinessException("导出excel异常:"+e.getMessage()); } finally { if (excelWriter != null) { excelWriter.finish(); } logger.info("导出excel耗时:" + (System.currentTimeMillis()-startTime)/1000 + "s"); }
}
|