首先在pom文件引入依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.0</version> </dependency>
然后写一个测试类:
public class FileTest { public static void main(String[] args) throws IOException { File file = new File("C:\\Users\\cs\\Desktop\\test.docx"); FileInputStream fis = null; XWPFDocument document = null; XWPFWordExtractor extractor = null; fis = new FileInputStream(file); document = new XWPFDocument(fis); extractor = new XWPFWordExtractor(document); System.out.println(extractor.getText()); } }
其中XWPFDocument、XWPFWordExtractor是其依赖中的方法,运行代码,结果如下: