如何使用apache poi从java中的docx中删除包含数据的表格和段落 [英] How to remove tables and paragraphs containing data from docx in java using apache poi

查看:113
本文介绍了如何使用apache poi从java中的docx中删除包含数据的表格和段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单词模板,它有多个相似的表格和一些与放在它们之前的表格相关联的段落.根据数据量,我填充了一些表格,其他表格不需要,所以有段落.

I have a word template that has multiple similar tables and some paragraphs associated to those tables placed just before them. Depending on the amount of data, I populate some tables and others are not required, so are there paragraphs.

我需要删除这些表格和段落.正如您在图像中看到的,我需要删除表 2 及其段落 Table Parahgraph

I need to remove these tables and paragraphs. As you can see in the image, I need to remove Table 2 and its paragraph Table Parahgraph

请帮我怎么做.我尝试使用 document.removeBodyElement(pos) ,但没有帮助.

Please help me how to do it. I tried using document.removeBodyElement(pos) , but it does not help.

int startIndex = 0;
int endIndex = 0;
startIndex = doc.getPosOfTable(doc.getTables().get(0));
startIndex++;
endIndex = doc.getPosOfTable(doc.getTables().get(1));
System.out.println("startIndex "+ startIndex);
System.out.println("endIndex "+ endIndex);

for(int i=startIndex; i<=endIndex; i++){
    doc.removeBodyElement(i);
}

推荐答案

问题在于使用 removeBodyElement 移动其余元素并重新计算它们的索引.这意味着,如果您想删除元素 #4 到 #6(包括两个表格之间的空段落),那么在删除元素 #4(空行)之后,这是您的第二个 TABLE(而不是其标题段落)将成为元素#5,等等.基本上,这个循环通过两个元素(i+=2而不是i++)跳转,从而删除只删了你想要的一半,甚至删除了你不想删除的东西.

The problem is that using removeBodyElement shifts the rest of the elements and recalculates their indices. It means, that if you want to delete elements #4 to #6 (empty paragraph between two tables is included), then after deleting the element #4 (empty line), it is your second TABLE (and not its title paragraph) that will become the element #5, etc. Basically, this loop becomes jumping by two elements (i+=2 instead of i++), thus deleting only half of what you want, and even deleting something you don't want to delete.

因此,您只需颠倒循环顺序:

for ( int i = endIndex; i >= startIndex; i-- ) {
    System.out.println( "removing bodyElement #" + i );
    document.removeBodyElement( i );
}

我用模板测试过,和你的例子类似,效果很好!希望有帮助.

I've tested it with a template, similar to your example, it works fine! Hope it helps.

这篇关于如何使用apache poi从java中的docx中删除包含数据的表格和段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆