java 读取word文件

java读取word文档,获取文本内容,保留基本的换行格式。

 

java用POI对word进行解析。所需jar包,用maven引入

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.2-FINAL</version>
</dependency>

 

前端用webuploader上传控件,限制上传文件类型仅支持text和word.

1  accept: {
2             title: 'text',
3             extensions: 'txt,docx',
4             mimeTypes: 'text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
5         }

 

后台MultipartFile接收文件,根据ContentType区分文件类型,区分解析获取文件内容。

word解析:

1 File uFile = new File("tempFile.docx");
2 if(!uFile.exists()){
3    uFile.createNewFile();
4 }
5 FileCopyUtils.copy(file.getBytes(), uFile);
6 OPCPackage opcPackage = POIXMLDocument.openPackage("tempFile.docx");
7 POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
8 txt= extractor.getText();

 

txt为word的文本内容

posted @ 2017-03-27 08:35  chanjuan  阅读(13059)  评论(1编辑  收藏  举报