如何将元素列表添加到 Word 文档?
How can I add a list of Elements to a Word Document?
Elements links = getLinkList();
for (Element link : links) {
System.out.println(link.attr("href"));
}
我使用 JSoup 从网页中提取了 link 个元素。我现在想把每个 link 逐行写在 Word 文档中。我该怎么做?
更新: 正如下面迈克所展示的...
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
使用 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"));
}
Elements links = getLinkList();
for (Element link : links) {
System.out.println(link.attr("href"));
}
我使用 JSoup 从网页中提取了 link 个元素。我现在想把每个 link 逐行写在 Word 文档中。我该怎么做?
更新: 正如下面迈克所展示的...
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
使用 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"));
}