使用POI解析出word文件中的文字与图片

引入依赖

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>

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

示例代码:

//这里的类名不小心用了小写,懒得改了 凑合看吧
public class stream {
    public static void main(String[] args) throws Exception {
        stream tp=new stream();
        //.docx和doc文件的读取
        String content = tp.readWord("src\\main\\resources\\img\\实习感受.doc");
        System.out.println("content===="+content);

    }
    public String readWord(String path) {
        String buffer = "";
        try {
            if (path.endsWith(".doc")) {

                //提取图片
                FileInputStream is = new FileInputStream(path);
                final HWPFDocument hwpfDocument = new HWPFDocument(is);
                final PicturesTable picturesTable = hwpfDocument.getPicturesTable();
                final List<Picture> allPictures = picturesTable.getAllPictures();
                for(Picture picture:allPictures){
                    int i =0;
                    final FileOutputStream fileOutputStream = new FileOutputStream("src\\main\\resources\\img33\\"+ UUID.randomUUID().toString() +picture.suggestFullFileName());
                    i++;
                    final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    bufferedOutputStream.write(picture.getContent());
                    bufferedOutputStream.close();
                }
                is.close();
                //提取文字
                FileInputStream is1 = new FileInputStream(path);
                WordExtractor ex = new WordExtractor(is1);
                buffer = ex.getText();
                is1.close();
            } else if (path.endsWith("docx")) {

                int i = 0;
                //返回图片
                FileInputStream is = new FileInputStream(path);
                final XWPFDocument xwpfDocument = new XWPFDocument(is);
                final List<XWPFPictureData> allPictures = xwpfDocument.getAllPictures();
                for(XWPFPictureData picture : allPictures) {
                    final FileOutputStream fileOutputStream = new FileOutputStream("src\\main\\resources\\img33\\"+ UUID.randomUUID().toString() +picture.getFileName());
                    i++;
                    final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    bufferedOutputStream.write(picture.getData());
                    bufferedOutputStream.close();
                }
                //返回word中的字符串
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();
                opcPackage.close();
            } else {
                System.out.println("此文件不是word文件!");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值