码迷,mamicode.com
首页 > 其他好文 > 详细

读取word文档,暂时不能读取图片,案例代码

时间:2020-01-03 10:27:38      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:equal   beans   group   extract   try   row   nal   div   content   

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Wordfile {

    public static void main(String[] args) throws Exception {
        String path = "G:\\样题.doc";
        String context = readWord(path);
        System.out.println(context);
    }


    public static String readWord(String path) {
        InputStream is = null;
        String content = "";
        String suffix = path.substring(path.lastIndexOf(".") + 1);
        try {
            if (suffix.equals("doc")) {
                 // word 2003: 图片不会被读取
                is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);// is是WORD文件的InputStream
                content = ex.getText().trim()
            } else if (suffix.equals("docx")) {
               // word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                content = (extractor).getText().trim();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content;
    }
}
  <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>



        <!--2010年EXCEL需要的包-->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>

  

读取word文档,暂时不能读取图片,案例代码

标签:equal   beans   group   extract   try   row   nal   div   content   

原文地址:https://www.cnblogs.com/Koaler/p/12143476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!