Apache POI SXSSFWorkbook单元格样式不起作用

时间:2019-01-07 03:17:21

标签: apache-poi

这个问题困扰了我好几天。这是有关它的详细说明。
我想使用SXSSFWorkbook生成一个较大的excel(.xlsx)文件,该文件的内存占用较少。
这是代码

`public static CellStyle createCellStyle2(String fontColor, String backgroundColor, short dataFormat, Workbook workbook, Map<String, CellStyle> xssfCellStyleMap) throws Exception {
    SXSSFWorkbook sxssfWorkbook = (SXSSFWorkbook) workbook;
    String md5 = DigestUtils.md5Hex(fontColor + backgroundColor + dataFormat);
    CellStyle cellStyle = xssfCellStyleMap.get(md5);
    if (ObjectUtil.isEmpty(cellStyle)) {
        cellStyle = sxssfWorkbook.createCellStyle();
        cellStyle.setDataFormat(dataFormat);
        if (!ObjectUtil.isEmpty(backgroundColor)) {
            XSSFColor XSSFBackgroundColor = getXSSFColor(backgroundColor);
            cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

 cellStyle.setFillForegroundColor(XSSFBackgroundColor.getIndexed());
            }
            if (!ObjectUtil.isEmpty(fontColor)) {
                XSSFColor XSSFFontColor = getXSSFColor4Font(fontColor);
                org.apache.poi.ss.usermodel.Font rowXSSFFont = sxssfWorkbook.createFont();
                rowXSSFFont.setColor(XSSFFontColor.getIndexed());
                cellStyle.setFont(rowXSSFFont);
            } else {
                org.apache.poi.ss.usermodel.Font rowXSSFFont = sxssfWorkbook.createFont();
                cellStyle.setFont(rowXSSFFont);
            }
            xssfCellStyleMap.put(md5, cellStyle);
        }
        return cellStyle;
    }` 

public static XSSFColor getXSSFColor(String hexaColor) throws Exception {
        int r = Integer.parseInt((hexaColor.substring(0, 2)), 16);
        int g = Integer.parseInt((hexaColor.substring(2, 4)), 16);
        int b = Integer.parseInt((hexaColor.substring(4, 6)), 16);
        java.awt.Color color = new Color(r, g, b);
        return new XSSFColor(color);
    }

下面是最终生成的文件
enter image description here

实际上,我要这样归档
enter image description here

如果您知道任何原因。请给我建议。非常感谢!

0 个答案:

没有答案
相关问题