《《《Java使用POI读取Word中的特殊表格》》》

转载地址https://www.cnblogs.com/fanwenhao/p/11096596.html

 

代码

 1 package live.autu.word;
 2  
 3 import java.io.FileInputStream;
 4  
 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  
14 /**
15  * Hello world!
16  *
17  */
18  
19 public class App {
20     public static void main(String[] args) {
21         //doc文档路径
22         String filePath = "C:\\Users\\autu\\Desktop\\test.doc";
23         //test.print(filePath,"第一个表");
24          
25         System.out.println(App.read(filePath,"第一个表"));;
26     }
27  
28     /**
29      * 读取文档中表格
30      * @param filePath doc路径
31      * @param set 第几个表格
32      */
33     public static String read(String filePath,String tableName) {
34  
35         StringBuilder sb=new StringBuilder();
36          
37          
38         try (FileInputStream in = new FileInputStream(filePath); // 载入文档
39                 POIFSFileSystem pfs = new POIFSFileSystem(in);
40                 HWPFDocument hwpf = new HWPFDocument(pfs);) {
41           
42             Range range = hwpf.getRange();// 得到文档的读取范围
43             TableIterator it = new TableIterator(range);
44             // 迭代文档中的表格
45       
46             while (it.hasNext()) {
47                 Table tb = (Table) it.next();
48               
49                 // 迭代行,默认从0开始,可以依据需要设置i的值,改变起始行数,也可设置读取到那行,只需修改循环的判断条件即可
50                 outer:for (int i = 0; i < tb.numRows(); i++) {
51                     TableRow tr = tb.getRow(i);
52                     // 迭代列,默认从0开始
53                     for (int j = 0; j < tr.numCells(); j++) {
54                         TableCell td = tr.getCell(j);// 取得单元格
55                         // 取得单元格的内容
56                         for (int k = 0; k < td.numParagraphs(); k++) {
57                             Paragraph para = td.getParagraph(k);
58                             String s = para.text();
59                             // 去除后面的特殊符号
60                             if (null != s && !"".equals(s)) {
61                                 s = s.substring(0, s.length() - 1);
62                             }
63                             s=s.trim();
64                             if(tableName.trim().equals(s)||i!=0) {
65                                 sb.append(s + "\t");
66                             } else {
67                                 break outer;
68                             }   
69                         }
70                     }
71                     sb.append( "\n");
72                 }
73             }
74  
75         } catch (Exception e) {
76             e.printStackTrace();
77         }
78          
79         return sb.toString();
80     }
81  
82 }

依赖

 1 <dependency>
 2     <groupId>org.apache.poi</groupId>
 3     <artifactId>poi</artifactId>
 4     <version>4.0.1</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>org.apache.poi</groupId>
 8     <artifactId>poi-scratchpad</artifactId>
 9     <version>4.0.1</version>
10 </dependency>

效果

 

posted @ 2020-04-17 13:44  贩卖长江水  阅读(565)  评论(0编辑  收藏  举报