怎么能以编程方式使用Apache的POI读取一个PowerPoint presentation图中的值? [英] How can one programmatically read the graph values from a Powerpoint presentation using Apache's POI?
问题描述
我有带图的简报presentation,我想用Java和 Apache的POI 。当我编辑使用PowerPoint的Excel窗口的价值观打开图形数据,我想从我的Java应用程序访问这些值。
I have a Powerpoint presentation with an graph which I want to access using Java and Apache's POI. When I edit the graph data using Powerpoint an Excel window opens with the values, I want to access these values from my Java application.
如何一个访问的图的值编程
How does one access the values of the graph programmatically?
推荐答案
在我们需要导航到 XSLFChart
对象的第一部分:
In the first part we need to navigate to an XSLFChart
object:
final String filename = "resources/fptbenchmark/Powerpoint Import.pptx";
final XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filename));
final XSLFSlide slide = ppt.getSlides()[5];
幻灯片包含不同部分( getRelations()
)其中一个应
包含 XSLFChart
:
final List<POIXMLDocumentPart> relations = slide.getRelations();
assert relations.size() == 3;
final XSLFChart xslfChart = (XSLFChart)relations.get(2);
当您检查调试器中的 xslfChart
变量,你会发现
该字段 CTChartImpl图
显示底层的XML数据,这
可能是这样的:
When you examine the xslfChart
variable in the debugger you will notice
that the field CTChartImpl chart
shows the underlying XML data, which
might look like this:
<xml-fragment xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<c:autoTitleDeleted val="0"/>
<c:plotArea>
<c:scatterChart>
<c:ser>
<c:tx>
<c:strRef>
<c:f>Sheet1!$E$8</c:f>
<c:strCache>
<c:ptCount val="1"/>
<c:pt idx="0">
<c:v>y axis caption</c:v>
</c:pt>
</c:strCache>
</c:strRef>
</c:tx>
<c:xVal>
<c:numRef>
<c:f>Sheet1!$A$9:$A$28</c:f>
<c:numCache>
<c:formatCode>General</c:formatCode>
<c:ptCount val="20"/>
<c:pt idx="0">
<c:v>1200</c:v>
</c:pt>
<c:pt idx="1">
<c:v>1600</c:v>
</c:pt>
<c:pt idx="2">
<c:v>2000</c:v>
</c:pt>
...
您可以naviage这棵树开始使用 CTChart
:
You can naviage this tree starting with the CTChart
:
CTChart ctChart = xslfChart.getCTChart();
既然有 c为C:plotArea&GT;
标签,你可以调用相关联的成员函数
访问:
Since there is a <c:plotArea>
tag, you call the associated member function to
access it:
CTPlotArea plotArea = ctChart.getPlotArea();
从那里,你应该能够浏览周围的路。
From there you should be able to navigate your way around
List<CTNumVal> ptList = plotArea.getScatterChartList().get(1)
.getSerList().get(0)
.getXVal()
.getNumRef()
.getNumCache()
.getPtList();
现在您可以访问的值:
ptList.get(0).getV();
参考
- ooxml-schemas-1.1.jar
- Ecma-376
这篇关于怎么能以编程方式使用Apache的POI读取一个PowerPoint presentation图中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!