我们在 Web 应用程序中使用 Apache POI 已有“多年”了。突然,从我们的应用程序创建的新文件和旧文件都不会显示折线图。现在我们使用的是 5.2.3,但是今年春天创建的文件,以前可以打开,也不会再打开了!?我遇到的唯一“错误”是在 Excel 中,它显示
Removed part: /x1/drawings/drawing1.xml
(所以其他一切看起来都应该如此)
如果我在(Apple)Numbers 中打开折线图,甚至在(Mac)Finder 中的“预览”中打开它,我就可以很好地看到折线图。
(应该)生成图表的代码是:
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, anchorR, 15, anchorR+20);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleOverlay(false);
chart.setTitleText(title);
List<Integer> selected = selectedMap.get("integer");
XDDFChartAxis bottomAxis = chart.createCategoryAxis(org.apache.poi.xddf.usermodel.chart.AxisPosition.BOTTOM);
bottomAxis.setTitle("");
XDDFValueAxis leftAxis = chart.createValueAxis(org.apache.poi.xddf.usermodel.chart.AxisPosition.LEFT);
leftAxis.setTitle("");
XDDFDataSource<String> ticks_xaxis = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(headerRow, headerRow, startCol, noOfCol));
Map<Integer, String> lineNames = new HashMap<>();
int count_=0;
XDDFChartData lc = null;
if (!selected.isEmpty()) {
List<XDDFNumericalDataSource<Double>> data = new ArrayList<>();
for (Integer integer : selected) {
int datar = startRow + integer;
data.add(XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(datar, datar, startCol, noOfCol)));
String lname = sheet.getRow(datar).getCell(0).getStringCellValue();
lineNames.put(count_++, lname);
}
lc = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
for (int d=0;d<data.size();d++) {
XDDFNumericalDataSource<Double> line = data.get(d);
XDDFLineChartData.Series ser = (XDDFLineChartData.Series) lc.addSeries(ticks_xaxis, line);
ser.setTitle(lineNames.get(d), null);
ser.setSmooth(true);
ser.setMarkerStyle(MarkerStyle.NONE);
}
chart.plot(lc);
}
表格没有问题(有系列试图“读取”的数据)等等..
我知道很难看出问题出在哪里,因为我无法给你完整的源代码,但我希望有人以前遇到过这个问题!我试图看看是否有一些依赖关系会搞乱任何事情。所以..任何提示,我洗耳恭听!
当前使用 Apache POI 的所有图表问题
Apache poi MS excel 损坏 -> 在包含条形图时打开使用 Apache POI 创建的文档时出错 -> 在最新版本的 Excel 中打开包含使用 Apache POI 生成的条形图的工作簿时出现错误 ->https:// stackoverflow.com/questions/77131316/create-a-chart-in-a-powerpoint-using-apache-poi-and-java-from-scratch/77135432#77135432
好像是这样的:
Apache POI 从版本 4.1.2 开始决定将数字格式设置默认设置为类别轴。但它设置一个空字符串作为数字格式代码。 Microsoft Office 2021 版之前的版本具有足够的容忍度,可以忽略空字符串数字格式。 Microsoft Office 365 则没有那么宽容。
不清楚为什么类别轴默认具有该设置。数值轴没有。
但要使其再次工作,必须修复不正确的数字格式设置。
在上面的代码中
bottomAxis
是类别轴。那是文本而不是数字。所以数字格式应该是@
,也就是文本。 leftAxis
是一个值轴。如果设置的话,可以采用数字格式 #,##0.00
。
因此更改上面的代码:
...
//repair axes number format settings
if (bottomAxis.hasNumberFormat()) bottomAxis.setNumberFormat("@");
if (leftAxis.hasNumberFormat()) leftAxis.setNumberFormat("#,##0.00");
...