Apache POI:从Word模板文件中完全删除图表

伊沃·斯特姆

我正在将Excel图表与Word模板一起使用,希望通过Java Apache POI库以编程方式对其进行操作。为此,我还需要能够有条件地删除存储在此模板中的图表。

基于Axel Richters的帖子(使用Apache POI从PowerPoint幻灯片中删除图表),我想我已经快到了,但是当我想打开更新的Word文件时,它给出了一个内容不可读的错误。到目前为止,这是我所拥有的:

PackagePart packagePartChart = xWPFChart.getPackagePart();

PackagePart packagePartWordDoc = xWPFDocument.getPackagePart();
OPCPackage packageWordDoc = packagePartWordDoc.getPackage();

// iterate over all relations the chart has and remove them
for (PackageRelationship chartrelship : packagePartChart.getRelationships()) {
    String partname = chartrelship.getTargetURI().toString();
    PackagePart part = packageWordDoc.getPartsByName(Pattern.compile(partname)).get(0);
    packageWordDoc.removePart(part);
    packagePartChart.removeRelationship(chartrelship.getId());
}

// now remove the chart itself from the word doc                           
Method removeRelation = POIXMLDocumentPart.class.getDeclaredMethod("removeRelation", POIXMLDocumentPart.class); 
removeRelation.setAccessible(true); 
removeRelation.invoke(xWPFDocument, xWPFChart);

如果我解压缩Word文件,我会正确看到:

  1. WordDoc和图表之间的关系将在'\ word \ _rels \ document.xml.rels'中删除
  2. 图表本身在文件夹“ \ word \ charts”中删除
  3. 支持图表本身的文档之间的关系在文件夹'\ word \ charts \'_rels中删除
  4. 相关图表项目本身将被删除:
    • 文件夹'\ word \ charts'中的StyleN / ColorsN和
    • 文件夹'\ word \ embeddings'中的Microsoft_Excel_WorksheetN

有人对这里可能出什么问题有任何想法吗?

伊沃·斯特姆

确实,找到包含图表的正确段落是一个挑战。最后,为简单起见,我添加了一个单行一列的占位符表,其中一个单元格带有文本,我们在表中直接说“ targetWordString”。通过以下功能,我可以找到此表的BodyElementID:

private Integer iBodyElementIterator (XWPFDocument wordDoc,String targetWordString) {
    Iterator<IBodyElement> iter = wordDoc.getBodyElementsIterator();
    Integer bodyElementID = null;
    while (iter.hasNext()) {
       IBodyElement elem = iter.next();
       bodyElementID = wordDoc.getBodyElements().indexOf(elem);
       if (elem instanceof XWPFParagraph) {

           XWPFParagraph paragraph = (XWPFParagraph) elem;
           for (XWPFRun runText : paragraph.getRuns()) {
                String text = runText.getText(0);                               
               Core.getLogger("WordExporter").trace("Body Element ID:  " + bodyElementID + " Text: " + text);
                if (text != null && text.equals(targetWordString)) {                        
                    break;                          
                }
            }

       } else if (elem instanceof XWPFTable) {
           if (((XWPFTable) elem).getRow(0) != null && ((XWPFTable) elem).getRow(0).getCell(0) != null) {
                // the first cell holds the name via the template
                String tableTitle = ((XWPFTable) elem).getRow(0).getCell(0).getText();
                if (tableTitle.equals(targetWordString)) {
                    break;
                }
                Core.getLogger("WordExporter").trace("Body Element ID:  " + bodyElementID + " Text: " + tableTitle);
           } else {
               Core.getLogger("WordExporter").trace("Body Element ID:  " + bodyElementID + " Table removed!");
           }

       }
       else {
           Core.getLogger("WordExporter").trace("Body Element ID:  " + bodyElementID + " Text: ?");
       }
    }
    return bodyElementID;
}

在代码的主要部分中,我调用此函数来查找表,然后首先删除图表(ID +1),然后删除表(ID)

int elementIDToBeRemoved = iBodyElementIterator(xWPFWordDoc,targetWordString);

xWPFWordDoc.removeBodyElement(elementIDToBeRemoved + 1);
xWPFWordDoc.removeBodyElement(elementIDToBeRemoved);

它必须按此顺序排列,因为一旦删除中间的数字,则ID会重新排序,因此首先删除表格,这意味着图表原则上将获得该ID。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过apache poi读取excel文件(在classpath中)

来自分类Dev

通过apache poi读取excel文件(在classpath中)

来自分类Dev

Apache POI Word文档空指针异常

来自分类Dev

Apache Poi更新XLS文件

来自分类Dev

如何使用Apache POI删除行

来自分类Dev

删除最后一行apache poi

来自分类Dev

使用Apache POI更改Word文件中的特定行

来自分类Dev

如何通过Apache poi设置Word文件的全局字体?

来自分类Dev

使用Apache POI在Excel中删除多行

来自分类Dev

使用Apache POI识别Word女士中的项目符号

来自分类Dev

Apache POI在Word文档中添加表格

来自分类Dev

通过Java Apache POI在Excel文件中追加新行

来自分类Dev

Apache POI和文件删除类

来自分类Dev

如何在Apache word poi(XWPF)中更改表方向?

来自分类Dev

使用Apache POI API从Excel文件中读取值

来自分类Dev

apache poi Word文档(.doc,.docx)更新

来自分类Dev

如何使用Apache POI删除行

来自分类Dev

Apache POI:在Word文档中查找不带空格的字符

来自分类Dev

使用Apache POI更改Word文件中的特定行

来自分类Dev

如何通过Apache poi设置Word文件的全局字体?

来自分类Dev

使用Apache POI识别Word女士中的项目符号

来自分类Dev

使用Apache POI编辑Word文档

来自分类Dev

Apache POI和文件删除类

来自分类Dev

如何在Apache word poi(XWPF)中更改表方向?

来自分类Dev

Apache Cordova中的HTML模板?

来自分类Dev

用apache poi在Android中覆盖后,Excel文件损坏

来自分类Dev

Apache poi 文件已损坏

来自分类Dev

使用 Apache POI Word JAVA 编写 docx 文件

来自分类Dev

如何通过Apache POI设置Excel Sunburst图表中各个数据标签的文本属性?