Java word 内容读取

1、添加依赖关系(网上好多帖子没有写依赖,害我找半天)

<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>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.0</version>
   </dependency>

不是maven项目到这里找JAR包,在页面中找“Files”右边就是“jar”点击即可下载
http://mvnrepository.com/artifact/org.apache.poi/poi/4.0.0

http://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/4.0.0

http://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad/4.0.0

2、写工具类

package com.xxx.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
import org.apache.poi.hwpf.extractor.WordExtractor;
 
public class DocUtil {
    /**
     * 读取doc文件内容
     * 
     * @param file
     *            想要读取的文件对象
     * @return 返回文件内容
     * @throws IOException
     */
    public static String doc2String(FileInputStream fs) throws IOException {
        StringBuilder result = new StringBuilder();
        WordExtractor re = new WordExtractor(fs);
        result.append(re.getText());
        re.close();
        return result.toString();
    }
 
    public static String doc2String(File file) throws IOException {
        return doc2String(new FileInputStream(file));
    }
 
    public static void main(String[] args) {
        File file = new File("C:\\Users\\frank.li\\Desktop\\产品购销合同范本1.doc");
        try {
            System.out.println(doc2String(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3、运行结果

4、word内容截图

posted @ 2019-06-26 14:28  古语云  阅读(4258)  评论(0编辑  收藏  举报