apache poi 修改docx表格_如何使用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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache POI提供了一种将HTML文件转换为Word文档的方法。下面是一个简单的示例代码: ```java import java.io.*; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.HtmlDocumentFacade; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import org.apache.poi.hwpf.usermodel.Range; public class HtmlToWordConverter { public static void main(String[] args) throws Exception { // 读取HTML文件 FileInputStream fis = new FileInputStream("input.html"); BufferedReader in = new BufferedReader(new InputStreamReader(fis, "UTF-8")); // 创建Word文档对象 HWPFDocument doc = new HWPFDocument(); Range range = doc.getRange(); // 将HTML文件转换为Word文档 WordToHtmlConverter converter = new WordToHtmlConverter(doc); converter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> { // 保存图片 try (FileOutputStream out = new FileOutputStream(suggestedName)) { out.write(content); } catch (IOException e) { e.printStackTrace(); } // 将图片插入Word文档 try { range.insertAfter(converter.createPicture(suggestedName, widthInches, heightInches)); } catch (Exception e) { e.printStackTrace(); } }); converter.processDocument(new HtmlDocumentFacade(fis)); // 保存Word文档 FileOutputStream fos = new FileOutputStream("output.doc"); doc.write(fos); fos.close(); } } ``` 在上面的代码,我们首先读取HTML文件,然后创建一个空的Word文档对象,并获取文档的范围。接着,我们创建一个WordToHtmlConverter对象,并设置其PicturesManager属性,以便将HTML文件的图片保存到本地,并将图片插入Word文档。最后,我们将HTML文件转换为Word文档,并将其保存到本地。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值