From 4fabfe4dbd2eb28d07a4350597d314958cc1c281 Mon Sep 17 00:00:00 2001 From: MrShi <1878285526@qq.com> Date: 星期四, 09 十月 2025 11:16:43 +0800 Subject: [PATCH] 优化 --- server/services/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 115 insertions(+), 1 deletions(-) diff --git a/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java b/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java index d2fac34..c6090e0 100644 --- a/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java +++ b/server/services/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java @@ -1,7 +1,9 @@ package com.doumee.core.annotation.excel; +import com.doumee.core.constants.Constants; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; +import com.doumee.dao.business.model.Goodsorder; import lombok.AllArgsConstructor; import lombok.Data; import org.apache.poi.ss.usermodel.*; @@ -168,6 +170,89 @@ } } +// public void exportList (List<List<String>> data, String fileName, String sheetName, HttpServletResponse response) { +// try { +// String encodeFileName = URLEncoder.encode(fileName, Charset.forName("UTF-8").toString()) + ".xlsx"; +// response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName); +// response.setContentType("application/octet-stream"); +// response.setHeader("eva-opera-type", "download"); +// response.setHeader("eva-download-filename", encodeFileName); +// this.exportList(data, sheetName, response.getOutputStream()); +// } catch (IOException e) { +// throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e); +// } +// } + + + /** + * 瀵煎嚭鍒版寚瀹氳緭鍑烘祦 + * @param os 杈撳嚭娴� + */ + public static void exportList (List<List<String>> dataList ,String sheetName, OutputStream os) { + SXSSFWorkbook sxssfWorkbook; + try { + sxssfWorkbook = new SXSSFWorkbook(); + Sheet sheet = sxssfWorkbook.createSheet(sheetName); + // 鍒涘缓鍒楀ご + sheet.createFreezePane(0, 1); + Row header = sheet.createRow(0); + CellStyle hstyle = configHeaderCellStatic(sxssfWorkbook); + for (int i = 0; i < dataList.size(); i++) { + Cell cell = header.createCell(i); + cell.setCellValue(dataList.get(i).get(Constants.ZERO)); + // 鍒楀璁剧疆 + sheet.setColumnWidth(i, dataList.get(i).get(Constants.ZERO).length() * 2 * 256); + // 璁剧疆鍒楀ご鍗曞厓鏍� + cell.setCellStyle(hstyle); + } + + //鎬昏鏁� + Integer rowSize = dataList.get(Constants.ZERO).size(); + //鎬诲垪鏁� + Integer columnSize = dataList.size(); + for (int i = 1; i < rowSize; i++) { + Row row = sheet.createRow(i); + for (int j = 0; j < columnSize; j++) { + Cell cell = row.createCell(j); + cell.setCellValue(dataList.get(j).get(i)); + } + } +// +// List<String> headerList =dataList.get(0); +// for (int i = 0; i < headerList.size(); i++) { +// Cell cell = header.createCell(i); +// cell.setCellValue(headerList.get(i)); +// // 鍒楀璁剧疆 +// sheet.setColumnWidth(i, headerList.get(i).length() * 2 * 256); +// // 璁剧疆鍒楀ご鍗曞厓鏍� +// cell.setCellStyle(hstyle); +// } +// // 鍒涘缓鏁版嵁璁板綍 +// for (int rowIndex = 1; rowIndex < dataList.size(); rowIndex++) { +// Row row = sheet.createRow(rowIndex ); +// List<String> rowList = dataList.get(rowIndex); +// for (int i = 0; i < rowList.size(); i++) { +// Cell cell = row.createCell(i); +// cell.setCellValue(rowList.get(i)); +// // 鍒楀璁剧疆 +// cell.setCellStyle(cstyle); +// } +// } + sxssfWorkbook.write(os); + os.close(); + } catch (Exception e) { + throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + /** * 瀵煎嚭鑷冲搷搴旀祦 * @param data 鏁版嵁 @@ -292,6 +377,35 @@ configCellBorder(style); cell.setCellStyle(style); } + private static CellStyle configHeaderCellStatic (SXSSFWorkbook workbook) { + CellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + // 璁剧疆鑳屾櫙 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + // 瀛椾綋 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 12); + style.setFont(font); + // 璁剧疆杈规 + configCellBorder(style); + return style; + } + private static CellStyle configCellStatic (SXSSFWorkbook workbook) { + CellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + // 璁剧疆鑳屾櫙 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + // 瀛椾綋 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 12); + style.setFont(font); + // 璁剧疆杈规 + configCellBorder(style); + return style; + } /** * 閰嶇疆鍒楀ご鍗曞厓鏍� */ @@ -332,7 +446,7 @@ /** * 閰嶇疆鍗曞厓鏍艰竟妗� */ - private void configCellBorder (CellStyle style) { + private static void configCellBorder (CellStyle style) { style.setBorderTop(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); -- Gitblit v1.9.3