如何向Word文档添加元素列表?

时间:2017-05-10 10:24:16

标签: java ms-word elements

Elements links = getLinkList();

for (Element link : links) {
    System.out.println(link.attr("href"));
}

我有一个使用JSoup从网页中提取的元素链接。我现在想逐行编写Word文档中的每个链接。我怎么能这样做?

更新:如下面的Mike所示......

private static void createSimpleDocument(Elements links) throws Exception {
    XWPFDocument document = new XWPFDocument();
    XWPFParagraph tmpParagraph = document.createParagraph();

    for (Element link : links) {
        XWPFRun tmpRun = tmpParagraph.createRun();
        String linkText = link.attr("href");
        tmpRun.setText(linkText);
        tmpRun.addBreak();
        tmpRun.addBreak();
    }

    FileOutputStream out = new FileOutputStream("...");
    document.write(out);
    out.close();
}

我能够成功保存和阅读文档,但是出于POI处理的目的,我需要将其另存为OLE2 Office文档。否则我收到此错误:

The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents

1 个答案:

答案 0 :(得分:1)

使用Apache POI https://poi.apache.org/

Elements links = getLinkList();
XWPFDocument document = new XWPFDocument();   
XWPFParagraph tmpParagraph = document.createParagraph();  
for (Element link : links) {
  XWPFRun tmpRun = tmpParagraph.createRun();   
  tmpRun.setText(link.attr("href"));   
}