我的目的是将条件格式规则从一个工作表复制到另一个工作表,其中两个工作表通过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);
}
除了我无法在目标表单中设置条件格式单元格的字体颜色外,一切正常。
我发现了两件事;
1st:源表的colorIndex总是变为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)
我试图几乎到处搜索原因,但找不到解决方案。
如果我做错了,你能提一下吗?