[英]How to read text in table from word document with java
我正在嘗試閱讀帶有 java 的 word 文檔.docx中的所有文本(也包括表格中的文本)。使用此代碼,我只能閱讀表格外的文本:
try{
FileInputStream fis = new FileInputStream(Path_to_Word_Document)
XWPFDocument document = new XWPFDocument(OPCPackage.open(fis))
List<XWPFParagraph> paragraphList = document.getParagraphs();
for(XWPFParagraph paragraph : paragraphList){
if(paragraph.getText().contains("X-01234-64572")){
System.out.println(paragraph.getText().contains("X-01234-64572")
} else {
System.out.println(System.out.println(paragraph.getText().contains("X-01234-64572")
}
}
}
試試下面的代碼,它會返回表格單元格的內容
XWPFDocument doc = new XWPFDocument(new FileInputStream(FILE_PATH));
for(XWPFTable table : doc.getTables()) {
for(XWPFTableRow row : table.getRows()) {
for(XWPFTableCell cell : row.getTableCells()) {
System.out.println("cell content: " + cell.getText());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.