k94314517
2025-05-06 2c4ea94474ce8f9eb48b71fb9960a7da45320282
server/system_service/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.core.utils.Constants;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
@@ -359,7 +360,7 @@
    /**
     * 配置单元格边框
     */
    private void configCellBorder (CellStyle style) {
    private static void configCellBorder(CellStyle style) {
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
@@ -428,4 +429,71 @@
        private Field field;
    }
    /**
     * 导出到指定输出流
     * @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));
                }
            }
            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();
                }
            }
        }
    }
    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;
    }
}