diff --git a/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/ExcelUtil.java b/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/ExcelUtil.java index 2e6c4a6..01156ed 100644 --- a/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/ExcelUtil.java +++ b/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/ExcelUtil.java @@ -73,6 +73,28 @@ public class ExcelUtil { } } + /** + * 指定名称、sheet名、title名、数据下载报表 + * + * @param fileName 文件名 + */ + public static void exportExcel(String fileName, String sheetName, String title, Class pojoClass, Collection dataSet) { + HttpServletResponse response = HttpServletUtil.getResponse(); + try (ServletOutputStream outputStream = response.getOutputStream()) { + fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8); + response.reset(); + response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); + response.setContentType("application/octet-stream;charset=UTF-8"); + ExportParams exportParams = new ExportParams(); + exportParams.setSheetName(sheetName); + exportParams.setTitle(title); + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, dataSet); + workbook.write(outputStream); + } catch (IOException e) { + log.error(">>> 导出数据异常:{}", e.getMessage()); + } + } + /** * 指定名称、sheet名(多个sheet)、数据(多组数据)下载报表 * @@ -115,7 +137,7 @@ public class ExcelUtil { cellStyle.setDataFormat(format.getFormat("@")); for (PullDown pullDown : pullDowns) { if (pullDown.getIsText()) { - ExcelUtil.selectListText(workbook, pullDown.getFirstCol(),cellStyle); + ExcelUtil.selectListText(workbook, pullDown.getFirstCol(), cellStyle); } else { String colName = numberToExcelColumn(num); int sum = pullDown.getStrings().stream().mapToInt(String::length).sum(); @@ -191,17 +213,17 @@ public class ExcelUtil { } /** - * @Description: * @param workbook * @param firstCol + * @Description: * @Author: wr * @Date: 2024/8/20 10:44 */ - public static void selectListText(Workbook workbook, int firstCol,CellStyle cellStyle) { + public static void selectListText(Workbook workbook, int firstCol, CellStyle cellStyle) { Sheet sheet = workbook.getSheetAt(0); for (int i = 2; i < 65535; i++) { Row row = sheet.getRow(i); - if(ObjectUtil.isNull(row)){ + if (ObjectUtil.isNull(row)) { row = sheet.createRow(i); } Cell cell = row.createCell(firstCol);