分享

Java使用poi读取word文档中的表格

 三十的狼 2019-11-25

使用poi读取文档中的表格,当有多个表格时可以指定需要读取的表格,同时支持读取docx和doc格式。需要添加poi的jar包

测试文档如下图:

程序代码: 

  1. package com.fise19.read;
  2. import java.io.FileInputStream;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import org.apache.poi.hwpf.HWPFDocument;
  6. import org.apache.poi.hwpf.usermodel.Paragraph;
  7. import org.apache.poi.hwpf.usermodel.Range;
  8. import org.apache.poi.hwpf.usermodel.Table;
  9. import org.apache.poi.hwpf.usermodel.TableCell;
  10. import org.apache.poi.hwpf.usermodel.TableIterator;
  11. import org.apache.poi.hwpf.usermodel.TableRow;
  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  13. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  14. import org.apache.poi.xwpf.usermodel.XWPFTable;
  15. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  16. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  17. /**
  18. *
  19. * 读取word文档中表格数据,支持doc、docx
  20. * @author Fise19
  21. *
  22. */
  23. public class ExportDoc {
  24. public static void main(String[] args) {
  25. ExportDoc test = new ExportDoc();
  26. String filePath = "D:\\new\\测试.docx";
  27. // String filePath = "D:\\new\\测试.doc";
  28. test.testWord(filePath);
  29. }
  30. /**
  31. * 读取文档中表格
  32. * @param filePath
  33. */
  34. public void testWord(String filePath){
  35. try{
  36. FileInputStream in = new FileInputStream(filePath);//载入文档
  37. // 处理docx格式 即office2007以后版本
  38. if(filePath.toLowerCase().endsWith("docx")){
  39. //word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后
  40. XWPFDocument xwpf = new XWPFDocument(in);//得到word文档的信息
  41. Iterator<XWPFTable> it = xwpf.getTablesIterator();//得到word中的表格
  42. // 设置需要读取的表格 set是设置需要读取的第几个表格,total是文件中表格的总数
  43. int set = 2, total = 4;
  44. int num = set;
  45. // 过滤前面不需要的表格
  46. for (int i = 0; i < set-1; i++) {
  47. it.hasNext();
  48. it.next();
  49. }
  50. while(it.hasNext()){
  51. XWPFTable table = it.next();
  52. System.out.println("这是第" + num + "个表的数据");
  53. List<XWPFTableRow> rows = table.getRows();
  54. //读取每一行数据
  55. for (int i = 0; i < rows.size(); i++) {
  56. XWPFTableRow row = rows.get(i);
  57. //读取每一列数据
  58. List<XWPFTableCell> cells = row.getTableCells();
  59. for (int j = 0; j < cells.size(); j++) {
  60. XWPFTableCell cell = cells.get(j);
  61. //输出当前的单元格的数据
  62. System.out.print(cell.getText() + "\t");
  63. }
  64. System.out.println();
  65. }
  66. // 过滤多余的表格
  67. while (num < total) {
  68. it.hasNext();
  69. it.next();
  70. num += 1;
  71. }
  72. }
  73. }else{
  74. // 处理doc格式 即office2003版本
  75. POIFSFileSystem pfs = new POIFSFileSystem(in);
  76. HWPFDocument hwpf = new HWPFDocument(pfs);
  77. Range range = hwpf.getRange();//得到文档的读取范围
  78. TableIterator it = new TableIterator(range);
  79. // 迭代文档中的表格
  80. // 如果有多个表格只读取需要的一个 set是设置需要读取的第几个表格,total是文件中表格的总数
  81. int set = 1, total = 4;
  82. int num = set;
  83. for (int i = 0; i < set-1; i++) {
  84. it.hasNext();
  85. it.next();
  86. }
  87. while (it.hasNext()) {
  88. Table tb = (Table) it.next();
  89. System.out.println("这是第" + num + "个表的数据");
  90. //迭代行,默认从0开始,可以依据需要设置i的值,改变起始行数,也可设置读取到那行,只需修改循环的判断条件即可
  91. for (int i = 0; i < tb.numRows(); i++) {
  92. TableRow tr = tb.getRow(i);
  93. //迭代列,默认从0开始
  94. for (int j = 0; j < tr.numCells(); j++) {
  95. TableCell td = tr.getCell(j);//取得单元格
  96. //取得单元格的内容
  97. for(int k = 0; k < td.numParagraphs(); k++){
  98. Paragraph para = td.getParagraph(k);
  99. String s = para.text();
  100. //去除后面的特殊符号
  101. if(null != s && !"".equals(s)){
  102. s = s.substring(0, s.length()-1);
  103. }
  104. System.out.print(s + "\t");
  105. }
  106. }
  107. System.out.println();
  108. }
  109. // 过滤多余的表格
  110. while (num < total) {
  111. it.hasNext();
  112. it.next();
  113. num += 1;
  114. }
  115. }
  116. }
  117. }catch(Exception e){
  118. e.printStackTrace();
  119. }
  120. }
  121. }

运行结果:

  1. 这是第2个表的数据
  2. 123 1 2 3
  3. 表2-1 1 2 3
  4. 表2-2 1 2 3

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多