個人博客 地址:https://www.wenhaofan.com/a/20190627135921
代碼
package live.autu.word;
import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
//doc文檔路徑
String filePath = "C:\\Users\\autu\\Desktop\\test.doc";
//test.print(filePath,"第一個表");
System.out.println(App.read(filePath,"第一個表"));;
}
/**
* 讀取文檔中表格
* @param filePath doc路徑
* @param set 第幾個表格
*/
public static String read(String filePath,String tableName) {
StringBuilder sb=new StringBuilder();
try (FileInputStream in = new FileInputStream(filePath); // 載入文檔
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);) {
Range range = hwpf.getRange();// 得到文檔的讀取范圍
TableIterator it = new TableIterator(range);
// 迭代文檔中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
// 迭代行,默認從0開始,可以依據需要設置i的值,改變起始行數,也可設置讀取到那行,只需修改循環的判斷條件即可
outer:for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 迭代列,默認從0開始
for (int j = 0; j < tr.numCells(); j++) {
TableCell td = tr.getCell(j);// 取得單元格
// 取得單元格的內容
for (int k = 0; k < td.numParagraphs(); k++) {
Paragraph para = td.getParagraph(k);
String s = para.text();
// 去除后面的特殊符號
if (null != s && !"".equals(s)) {
s = s.substring(0, s.length() - 1);
}
s=s.trim();
if(tableName.trim().equals(s)||i!=0) {
sb.append(s + "\t");
} else {
break outer;
}
}
}
sb.append( "\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
依賴
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.0.1</version> </dependency>
效果圖
test.doc

控制台打印

