poi获取段落位置_Apache POI:从java中的word文档(docx)中提取段落和后续表格
POI中的Word API仍然處于不穩定狀態,但是您應該能夠通過以下兩種方式之一迭代這些段落:
XWPFDocument doc = new XWPFDocument(fis);
List paragraphs = doc.getParagraphs();
for (XWPFParagraph p : paragraphs) {
... do something here
}
要么
XWPFDocument doc = new XWPFDocument(fis);
Iterator iter = doc.getParagraphsIterator();
while (iter.hasNext()) {
XWPFParagraph p = iter.next();
... do something here
}
Javadocs說XWPFDocument.getParagraphs()檢索在頁眉或頁腳中保存文本的段落,但我必須相信這是一個剪切和粘貼錯誤,因為XWPFHeaderFooter.getParagraphs()說同樣的事情.查看源代碼,XWPFDocument.getParagraphs()返回一個不可修改的列表,同時使用迭代器使段落可修改.這可能會在未來發生變化,但這是它現在的工作方式.
要檢索所有正文元素,段落和表的列表,您需要使用:
XWPFDocument doc = new XWPFDocument(fis);
Iterator iter = doc.getBodyElementsIterator();
while (iter.hasNext()) {
IBodyElement elem = iter.next();
if (elem instanceof XWPFParagraph) {
... do something here
} else if (elem instanceof XWPFTable) {
... do something here
}
}
這應該允許您按順序遍歷所有body元素.
總結
以上是生活随笔為你收集整理的poi获取段落位置_Apache POI:从java中的word文档(docx)中提取段落和后续表格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么nodejs是单进程的_Nodej
- 下一篇: radio 事件_nRF52832/51