alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
31.43k stars 7.44k forks source link

实现CellWriteHandler接口导出文件为xlsx样式代码格式不生效,导出格式为xls能生效 #3866

Open liuminminmin opened 3 days ago

liuminminmin commented 3 days ago

建议先去看文档

快速开始常见问题

触发场景描述

触发Bug的代码


   这里写代码
``` public static void main(String[] args) throws Exception {
        String fileName = "D:/excel/easyexcel.xlsx";

        writeExcel(null, new ArrayList<>(), fileName, "test", BonusSupplyInfoDTO.class);
    }
    public static void writeExcel(HttpServletResponse resp, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception {

        EasyExcel.write(fileName, clazz).sheet(sheetName)
                .registerWriteHandler(new CellWriteHandler() {
                    @Override
                    public void afterCellDispose(CellWriteHandlerContext context) {
                        if (context.getHead()) {
                            Cell cell = context.getCell();
                            String stringCellValue = cell.getStringCellValue();
                            Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
                            RichTextString richTextString = workbook.getCreationHelper().createRichTextString(stringCellValue);
                            Font font = workbook.createFont();
                            font.setColor(Font.COLOR_RED);
                            richTextString.applyFont(0, 1, font);
                            cell.setCellValue(richTextString);
                        }
                    }
                }).doWrite(data);
    }
# 提示的异常或者没有达到的效果
导出的文件格式为xlsx,代码设置的样式没生效,为xls时,导出样式会生效
LSL1618 commented 3 days ago

xls格式使用的字体样式类为org.apache.poi.ss.usermodel.Font,xlsx格式使用的字体样式类为org.apache.poi.xssf.usermodel.XSSFFont,参考如下:

        if (isHead) {
            Sheet sheet = writeSheetHolder.getSheet();
            Workbook workbook = sheet.getWorkbook();
            XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
            // 复制已存在的样式
            cellStyle.cloneStyleFrom(cell.getCellStyle());
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            // RGB颜色值
            //IndexedColorMap colorMap = ((XSSFWorkbook) workbook).getStylesSource().getIndexedColors();
            int red = 0, green = 0, blue = 0;
            XSSFColor color = new XSSFColor(new Color(red, green, blue), new DefaultIndexedColorMap());
            // 设置RGB背景颜色
            cellStyle.setFillForegroundColor(color);
            // 设置RGB字体颜色
            XSSFFont font = cellStyle.getFont();
            font.setColor(color);
            cellStyle.setFont(font);
            cellStyle.setDataFormat(49);
            cell.setCellStyle(cellStyle);
        }
liuminminmin commented 3 days ago

xls格式使用的字体样式类为org.apache.poi.ss.usermodel.Font,xlsx格式使用的字体样式类为org.apache.poi.xssf.usermodel.XSSFFont,参考如下:

        if (isHead) {
            Sheet sheet = writeSheetHolder.getSheet();
            Workbook workbook = sheet.getWorkbook();
            XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
            // 复制已存在的样式
            cellStyle.cloneStyleFrom(cell.getCellStyle());
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            // RGB颜色值
            //IndexedColorMap colorMap = ((XSSFWorkbook) workbook).getStylesSource().getIndexedColors();
            int red = 0, green = 0, blue = 0;
            XSSFColor color = new XSSFColor(new Color(red, green, blue), new DefaultIndexedColorMap());
            // 设置RGB背景颜色
            cellStyle.setFillForegroundColor(color);
            // 设置RGB字体颜色
            XSSFFont font = cellStyle.getFont();
            font.setColor(color);
            cellStyle.setFont(font);
            cellStyle.setDataFormat(49);
            cell.setCellStyle(cellStyle);
        }

还是没生效呀,我要实现的是把excel的表头的第一个字符变成红色