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

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

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

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

  1. r.setText(text, 0);

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

  1. for (XWPFTable tbl : doc.getTables()) {
  2. for (XWPFTableRow row : tbl.getRows()) {
  3. for (XWPFTableCell cell : row.getTableCells()) {
  4. for (XWPFParagraph p : cell.getParagraphs()) {
  5. for (XWPFRun r : p.getRuns()) {
  6. // ...
  7. }
  8. }
  9. // 替换具有嵌套表格的值
  10. for (XWPFTable tbl2 : cell.getTables()) {
  11. for (XWPFTableRow row2 : tbl2.getRows()) {
  12. for (XWPFTableCell cell : row.getTableCells()) {
  13. for (XWPFParagraph p : cell.getParagraphs()) {
  14. for (XWPFRun r : p.getRuns()) {
  15. // ...
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
英文:

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

  1. r.setText(text, 0);

For Table u need to find this way

  1. for (XWPFTableRow row : tbl.getRows()) {
  2. for (XWPFTableCell cell : row.getTableCells()) {
  3. for (XWPFParagraph p : cell.getParagraphs()) {
  4. for (XWPFRun r : p.getRuns()) {
  5. .....
  6. }}
  7. // Replace values with nested table
  8. for (XWPFTable tbl2 : cell.getTables()) {
  9. for (XWPFTableRow row2 : tbl2.getRows()){
  10. for (XWPFTableCell cell : row.getTableCells()) {
  11. for (XWPFParagraph p : cell.getParagraphs()) {
  12. for (XWPFRun r : p.getRuns()) {
  13. ...
  14. }}
  15. }}}

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:

确定