无法通过 apache poi 在条件格式单元格中设置字体颜色

KOU*_*DAL 5 java apache excel apache-poi

我的目的是将条件格式规则从一张工作表复制到另一张工作表,其中两张工作表通过 apache poi 不在同一个工作簿中。为此,我使用以下代码;

for(int i=0;i<sscf.getNumConditionalFormattings();i++){

        ConditionalFormatting cf = sscf.getConditionalFormattingAt(i);
        CellRangeAddress[] range = cf.getFormattingRanges();

        //getting various properties from source
        ConditionalFormattingRule rule = cf.getRule(0);

        PatternFormatting pattern = rule.getPatternFormatting();
        Color bgcolor = pattern.getFillBackgroundColorColor();
        Color fgcolor = pattern.getFillForegroundColorColor();
        Short fillPattern = pattern.getFillPattern();

        FontFormatting font = rule.getFontFormatting();
        int colorIndex = font.getFontColorIndex();
        Color fontColor = font.getFontColor();
        int fontHeight = font.getFontHeight();
        short ulType = font.getUnderlineType();

        //--------------------------------End of getting------------------------------

        //setting properties from source
        ConditionalFormattingRule newRule = dscf.createConditionalFormattingRule(rule.getFormula1());
        PatternFormatting newPattern = newRule.createPatternFormatting();
        FontFormatting newFont = newRule.createFontFormatting();

        if(bgcolor != null)
            newPattern.setFillBackgroundColor(bgcolor);

        if(fgcolor != null)
            newPattern.setFillForegroundColor(fgcolor);

        if(fillPattern != 0)
            newPattern.setFillPattern(fillPattern);

        if(colorIndex != 0)
            newFont.setFontColorIndex((short) colorIndex);

        if(fontColor != null)             
            newFont.setFontColor(fontColor);   //<-----This line

        if(fontHeight != 0)
            newFont.setFontHeight(fontHeight);

        if(ulType != 0)
            newFont.setUnderlineType(ulType);

        dscf.addConditionalFormatting(range,newRule);
    }
Run Code Online (Sandbox Code Playgroud)

一切正常,除了我无法在目标工作表中设置条件格式单元格的字体颜色

我注意到两件事;

第一:源表的颜色索引始终为 0(虽然我在源表中的字体颜色是绿色或红色)

第二:我在这条线指出的异常如下

java.lang.IndexOutOfBoundsException
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl.setColorArray(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFFontFormatting.setFontColor(XSSFFontFormatting.java:123)
at processor.ExcelTemplateProcessor.copyConditionalFormattingRules(ExcelTemplateProcessor.java:799)
at processor.ExcelTemplateProcessor.copySheet(ExcelTemplateProcessor.java:163)
at processor.ExcelTemplateProcessor.copySheet(ExcelTemplateProcessor.java:128)
at processor.ExcelTemplateProcessor.createReport(ExcelTemplateProcessor.java:117)
at service.TemplateReportService.createReport(TemplateReportService.java:189)
at test.Test.main(Test.java:62)
Run Code Online (Sandbox Code Playgroud)

我试图几乎到处搜索原因,但找不到解决方案。

如果我做错了什么,你能提一下吗?