How to remove tables and paragraphs containing data from docx in java using apache poi

纵饮孤独 提交于 2019-12-01 09:38:49

问题


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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/21968813/how-to-remove-tables-and-paragraphs-containing-data-from-docx-in-java-using-apac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!