1、pom.xml依赖
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2、读取文件类ReadDoc.java
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class ReadDoc {
public void testReadByDoc(String path) throws Exception {
InputStream is = new FileInputStream(path);
HWPFDocument doc = new HWPFDocument(is);
//输出书签信息
this.printInfo(doc.getBookmarks());
//输出文本
System.out.println(doc.getDocumentText());
Range range = doc.getRange();
this.printInfo(range);
//读表格
this.readTable(range);
//读列表
this.readList(range);
//把当前HWPFDocument写到输出流中
doc.write(new FileOutputStream("D:\\test.doc"));
is.close();
}
/**
* 输出书签信息
* @param bookmarks
*/
private void printInfo(Bookmarks bookmarks) {
int count = bookmarks.getBookmarksCount();
System.out