如何使用Apache的POI以编程方式从Powerpoint演示文稿中读取图形值?

Mic*_*ann 3 java powerpoint apache-poi

我有一个带有图表的Powerpoint演示文稿,我希望使用Java和Apache的POI进行访问.当我使用Powerpoint编辑图形数据时,将打开包含值的Excel窗口,我想从我的Java应用程序访问这些值.

如何以编程方式访问图形的值?

Mic*_*ann 5

在第一部分中,我们需要导航到一个XSLFChart对象:

final String filename = "resources/fptbenchmark/Powerpoint Import.pptx";
final XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filename));

final XSLFSlide slide = ppt.getSlides()[5];
Run Code Online (Sandbox Code Playgroud)

幻灯片包含不同的部分(getRelations())其中一部分应包含XSLFChart:

final List<POIXMLDocumentPart> relations = slide.getRelations();
assert relations.size() == 3;

final XSLFChart xslfChart = (XSLFChart)relations.get(2);
Run Code Online (Sandbox Code Playgroud)

xslfChart在调试器中检查变量时,您会注意到该字段CTChartImpl chart显示了基础XML数据,可能如下所示:

<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>
...
Run Code Online (Sandbox Code Playgroud)

您可以从以下位置开始导航此树CTChart:

CTChart ctChart = xslfChart.getCTChart();
Run Code Online (Sandbox Code Playgroud)

由于有<c:plotArea>标记,您可以调用关联的成员函数来访问它:

CTPlotArea plotArea = ctChart.getPlotArea();
Run Code Online (Sandbox Code Playgroud)

从那里你应该能够导航

List<CTNumVal> ptList = plotArea.getScatterChartList().get(1)
    .getSerList().get(0)
    .getXVal()
    .getNumRef()
    .getNumCache()
    .getPtList();
Run Code Online (Sandbox Code Playgroud)

现在您可以访问以下值:

ptList.get(0).getV();
Run Code Online (Sandbox Code Playgroud)

参考