如何使用Apache POI删除表和在java中的docx从包含数据的段落 [英] How to remove tables and paragraphs containing data from docx in java using apache poi
问题描述
我有一个具有多个类似表和相关的只是在他们面前摆放的那些表一些段落Word模板。根据数据量,我填充一些表和其他不是必需的,所以在那里段落。
我需要删除这些表和段落。
正如你可以在图片中看到,我需要删除表2和表款Parahgraph
请帮助我如何做到这一点。我试着用document.removeBodyElement(POS),但它并不能帮助。
INT的startIndex = 0;
INT endIndex的= 0;
的startIndex = doc.getPosOfTable(doc.getTables()得到(0));
的startIndex ++;
endIndex的= doc.getPosOfTable(doc.getTables()得到(1));
的System.out.println(的startIndex+的startIndex);
的System.out.println(endIndex的+ endIndex的);对于(INT I =的startIndex; I< = endIndex的;我++){
doc.removeBodyElement(ⅰ);
}
现在的问题是,使用 removeBodyElement
移动元素的其他部分,并重新计算其指数。这意味着,如果你想删除元素#4#6(包含两个表之间的空段落),然后删除该元素#4(空行)后,它是你的第二个表(而不是它的标题段落)的将成为元素#5等,基本上,这个循环变成跳跃的两个元素( I + = 2
,而不是我++
),从而删除只是你想要的一半,甚至删除你不想删除一些东西。
因此,你必须正好扭转你的循环顺序
的for(int i = endIndex的; I> =的startIndex;我 - ){
的System.out.println(删除bodyElement#+ I);
document.removeBodyElement(ⅰ);
}
我已经与模板类似,你的榜样测试它,它工作正常!希望它帮助。
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.
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
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);
}
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.
Thus, you have just to reverse the order of your loop:
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屋!