doum
2025-09-08 55ba702c1df240929e68df3b42fa9cb0607378b8
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);