jiangping
2025-03-18 dd8998a8f7bbf8823dba5fc219a961883e34ef7d
server/services/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java
@@ -2,6 +2,7 @@
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.*;
@@ -167,6 +168,54 @@
            }
        }
    }
    /**
     * 导出到指定输出流
     * @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);
            CellStyle cstyle = configCellStatic(sxssfWorkbook);
            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();
                }
            }
        }
    }
    /**
     * 导出至响应流
@@ -292,6 +341,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 +410,7 @@
    /**
     * 配置单元格边框
     */
    private void configCellBorder (CellStyle style) {
    private  static void configCellBorder (CellStyle style) {
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);