我正在尝试创建具有3级着色的XSSFConditionalFormattingRule
。因此,我也需要设置阈值。在调试后,我发现每个XSSFConditionalFormattingThreshold
在其CTCfvo
属性上抛出com.sun.jdi.InvocationException occurred invoking method.
,但仅在调用rule.getColorScaleFormatting().setNumControlPoints(3);
我的完整代码是:
CellRangeAddress[] regions = { CellRangeAddress.valueOf("Z2:Z" + (sheet.getLastRowNum() + 1)) };
XSSFConditionalFormattingRule rule = sheet.getSheetConditionalFormatting()
.createConditionalFormattingColorScaleRule();
XSSFConditionalFormattingThreshold thresh5 = rule.getColorScaleFormatting().createThreshold();
thresh5.setRangeType(RangeType.NUMBER);
thresh5.setValue(0.05);
XSSFConditionalFormattingThreshold thresh10 = rule.getColorScaleFormatting().createThreshold();
thresh10.setRangeType(RangeType.NUMBER);
thresh10.setValue(0.10);
XSSFConditionalFormattingThreshold thresh15 = rule.getColorScaleFormatting().createThreshold();
thresh15.setRangeType(RangeType.NUMBER);
thresh15.setValue(0.15);
rule.getColorScaleFormatting().setNumControlPoints(3);
rule.getColorScaleFormatting()
.setThresholds(new ConditionalFormattingThreshold[] { thresh5, thresh10, thresh15 });
XSSFColor colorGreen = new XSSFColor(IndexedColors.GREEN, colorMap);
XSSFColor colorYellow = new XSSFColor(IndexedColors.YELLOW, colorMap);
XSSFColor colorRed = new XSSFColor(IndexedColors.RED, colorMap);
rule.getColorScaleFormatting().setColors(new Color[] { colorGreen, colorYellow, colorRed });
sheet.getSheetConditionalFormatting().addConditionalFormatting(regions, rule);
这是缩短的stacktrace 我在出现上述代码时会得到:
org.apache.xmlbeans.impl.values.xmlvaluedisconnectedException at org.apache.xmlbeans.impl.values.xmlobjectbase.check_orphaned(xmlobjectbase.java:1258( 在 org.apache.xmlbeans.impl.values.xmlobjectbase.newcursor(xmlobjectbase.java:286( 在 org.apache.xmlbeans.impl.values.xmlcomplexcontentimpl.arraysetterhelper(xmlcomplexcontentimpl.java:1124( 在 org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.ctcolorscaleimpl.setcfvoarray(Unknown 来源( org.apache.poi.xssf.usermodel.xssfcolorscaleformatting.setthresholds(xssfcolorscaleformatting.java:85(
是什么原因导致我在这里看到的错误?当我创建XSSFConditionalFormattingThreshold
时,这是一个错误吗?还是完全其他的?
我正在使用apache poi v4.0.0。
正如问题所述,此错误的唯一发生归因于两次编写工作簿(所以问题,bugzilla(。所以这让我想知道我在做什么错。我没有真正的想法从哪里开始,因为与错误相关的所有内容都与我的问题无关。
我的第一个外观转到void org.apache.poi.xssf.usermodel.XSSFColorScaleFormatting.setNumControlPoints(int num)
的文档,该文档指出以下内容:
设置用于绘制颜色的控制点的数量。应该 通常是2或3。
更新后,您需要确保阈值计数和颜色 计数匹配
第二部分引起了我的注意。我想一些事情:
我用
getColorScaleFormatting().createThreshold()
直接在ColorScaleFormatting
上创建阈值。那么,如果setNumControlPoints()
实际重置以前创建的所有阈值怎么办?
这就是我在创建所有XSSFConditionalFormattingThreshold
之前简单地将rule.getColorScaleFormatting().setNumControlPoints(3);
移入的点,而我的代码正常工作。
使用setNumControlPoints
方法,我们可以看到:
public void setNumControlPoints(int num) {
while (num < _scale.sizeOfCfvoArray()) {
_scale.removeCfvo(_scale.sizeOfCfvoArray()-1);
_scale.removeColor(_scale.sizeOfColorArray()-1);
}
while (num > _scale.sizeOfCfvoArray()) {
_scale.addNewCfvo();
_scale.addNewColor();
}
}
这清楚地表明,调用此方法时可能会删除某些事情,尽管我无法完全了解_scale.sizeOfCfvoArray()
的初始大小是什么(我假设为0(。因此,我看不到如何删除东西,因为我创建了3个阈值(_scale.sizeOfCfvoArray()
应该是3个阈值(,然后我致电setNumControlPoints(3)
,所以我们将拥有num == _scale.sizeOfCfvoArray()
,而我找不到任何事情(虽然看起来完全删除了一切(。
得出结论,setNumControlPoints(int num)
应在创建规则和创建任何阈值之前始终称为第一件事。
num == _scale.sizeOfCfvoArray()
如果一切都可以重置的原因,请随时发表评论或编辑。