From 553fc4237a64bb1825ad39a6af4a9fe3d35b3b44 Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Tue, 21 Jan 2025 13:45:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=87=BA=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/njcn/web/utils/ExcelUtil.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) 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);