本文介绍了使用 Apache POI 4.0 运行官方示例 LineChars 和 ScatterChart 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方示例存在问题LineChart 和 ScatterChart 用于 Apache POI 4.0.它们编译和运行没有错误,但无法打开创建的 Excel 文件,说明存在不可读的内容.Excel 2010 &2016 年提供了从工作簿中恢复数据的选项,单击是"后,此 对话框 出现.可能是什么问题?

解决方案

新的XDDF代码缺少lineChartaxId的设置> 和 scatterChart.

/xl/charts/chart1.xml 这看起来像:

...<c:axId val="0"/><c:axId val="1"/></c:lineChart>

对于折线图..

添加:

...XDDFChartData 数据 = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);data.addSeries(xs, ys1);data.addSeries(xs, ys2);图表(数据);//将轴ID设置为LineChartchart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(bottomAxis.getId());chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(leftAxis.getId());//将输出写入文件试试 (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {wb.write(fileOut);}...

LineChart.java

...XDDFChartData 数据 = chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);data.addSeries(xs, ys1);data.addSeries(xs, ys2);图表(数据);//将轴 ID 设置为 ScatterChartchart.getCTChart().getPlotArea().getScatterChartArray(0).addNewAxId().setVal(bottomAxis.getId());chart.getCTChart().getPlotArea().getScatterChartArray(0).addNewAxId().setVal(leftAxis.getId());//将输出写入文件试试 (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {wb.write(fileOut);}...

ScatterChart.java

它会起作用.

There's a problem with the official examples LineChart and ScatterChart for Apache POI 4.0. They compile and run without errors, but the created Excel file cannot be opened stating that there is unreadable content. Excel 2010 & 2016 are giving the option to recover data from the workbook and after clickin' yes, this dialog appears. What might be the problem?

解决方案

The new XDDF code lacks the setting the axIds in the lineChart and scatterChart.

In /xl/charts/chart1.xml this looks like:

<c:lineChart>
 ...
 <c:axId val="0"/>
 <c:axId val="1"/>
</c:lineChart>

for a line chart..

Do adding:

...
            XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
            data.addSeries(xs, ys1);
            data.addSeries(xs, ys2);
            chart.plot(data);

            //setting the axis Ids to the LineChart
            chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(bottomAxis.getId());
            chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(leftAxis.getId());


            // Write the output to a file
            try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
                wb.write(fileOut);
            }
...

in LineChart.java

and

...
            XDDFChartData data = chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);

            data.addSeries(xs, ys1);
            data.addSeries(xs, ys2);
            chart.plot(data);

            //setting the axis Ids to the ScatterChart
            chart.getCTChart().getPlotArea().getScatterChartArray(0).addNewAxId().setVal(bottomAxis.getId());
            chart.getCTChart().getPlotArea().getScatterChartArray(0).addNewAxId().setVal(leftAxis.getId());


            // Write the output to a file
            try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
                wb.write(fileOut);
            }

...

in ScatterChart.java

and it will work.

这篇关于使用 Apache POI 4.0 运行官方示例 LineChars 和 ScatterChart 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 19:58