1、添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
2、工具类
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* 读取word文档中表格数据,支持doc、docx
* @author Fise19
*
*/
public class ReadWordUtil {
public static void main(String[] args) {
ReadWordUtil test = new ReadWordUtil();
String filePath = "C:/Users/00/Desktop/检测报告/123.doc";
// String filePath = "D:\\new\\测试.doc";
List<List<String>> lists = test.tableInWord(filePath, 1);
for(List<String> ss : lists){
for(String s : ss){
System.out.println(">>>>>>>>"+s);
}
}
}
/**
* 读取文档中表格
* @param filePath
*/
public static List<List<String>> tableInWord(String filePath,Integer orderNum){
try{
FileInputStream in = new FileInputStream(filePath);//载入文档
// 处理docx格式 即office2007以后版本
if(filePath.toLowerCase().endsWith("docx")){
//word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后
XWPFDocument xwpf = new XWPFDocument(in);//得到word文档的信息
Iterator<XWPFTable> itpre = xwpf.getTablesIterator();//得到wor