Java Apache poi: Word – 无法提取文档中带有编号和表格的特定文本

huangapple go评论73阅读模式
英文:

Java Apache poi: Word - Unable to extract specific texts from document along with numbering and tables

问题

无法从文档中提取特定文本,包括编号和表格。

有关如何解决这个问题的任何想法吗?

英文:

Unable to extract specific texts from document along with numbering and tables.

Any ideas on how to solve this?

答案1

得分: 1

你需要设置位置,仅替换具有以下格式的文本:

r.setText(text, 0);

对于表格,你需要按照以下方式查找:

for (XWPFTable tbl : doc.getTables()) {
    for (XWPFTableRow row : tbl.getRows()) {
        for (XWPFTableCell cell : row.getTableCells()) {
            for (XWPFParagraph p : cell.getParagraphs()) {
                for (XWPFRun r : p.getRuns()) {
                    // ...
                }
            }
            // 替换具有嵌套表格的值
            for (XWPFTable tbl2 : cell.getTables()) {
                for (XWPFTableRow row2 : tbl2.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph p : cell.getParagraphs()) {
                            for (XWPFRun r : p.getRuns()) {
                                // ...
                            }
                        }
                    }
                }
            }
        }
    }
}
英文:

You need to set with the position to replace only the text with format

r.setText(text, 0);

For Table u need to find this way

    for (XWPFTableRow row : tbl.getRows()) {
	 for (XWPFTableCell cell : row.getTableCells()) {
      for (XWPFParagraph p : cell.getParagraphs()) {
		for (XWPFRun r : p.getRuns()) {
         .....
        }}
        // Replace values with nested table 
        for (XWPFTable tbl2 : cell.getTables()) {
		 for (XWPFTableRow row2 : tbl2.getRows()){
          for (XWPFTableCell cell : row.getTableCells()) {
           for (XWPFParagraph p : cell.getParagraphs()) {
		    for (XWPFRun r : p.getRuns()) {
            ...
          }}
        }}}

huangapple
  • 本文由 发表于 2020年5月29日 12:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/62079017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定