前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POI读取第三方下载的Word文档

POI读取第三方下载的Word文档

作者头像
用户1215919
发布2021-12-28 12:42:21
5070
发布2021-12-28 12:42:21
举报
文章被收录于专栏:大大的微笑大大的微笑

因为从第三方读取到的word可能是其他格式(例如:html)转成word的,此时去读取word可能会失败。这里以HTML为例

依赖
代码语言:javascript
复制
 <!-- parse world -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>

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

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.3</version>
        </dependency>
代码片段
代码语言:javascript
复制
private String parseWord(String path) throws ParseWordException {
        // inspect
        if (isEmpty(path)) {
            throw new ParseWordException(Code.PARAM_EMPTY.getCode(), Code.PARAM_EMPTY.getMessage());
        }

        // reader
        File file = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        }  catch (FileNotFoundException e) {
            throw new ParseWordException(Code.READER_FILE_FAILURE.getCode(), Code.READER_FILE_FAILURE.getMessage());
        }


        // parse

        try {
            if (path.toUpperCase().endsWith(FileType.DOC.toString())) {
                HWPFDocument wordDoc = new HWPFDocument(fis);
                // 自己读

            } else if (path.toUpperCase().endsWith(FileType.DOCX.toString())) {
                XWPFDocument wordDocx = new XWPFDocument(fis);
                // 自己读

            } else {
                // 文件格式非法
                throw new ParseWordException(Code.FILE_TYPE_ILLEGAL.getCode(), Code.FILE_TYPE_ILLEGAL.getMessage());
            }

        }
        catch (IllegalArgumentException ie) {
            System.out.println(ie.getMessage());
            if (isEmpty(ie.getMessage())) {
                throw new ParseWordException(Code.PARAM_EMPTY.getCode(), Code.PARAM_EMPTY.getMessage());
            }
            if (ie.getMessage().contains("The document is really a HTML file")) {
                // 格式转换
                try {
                    String htmlPath = parseHtml(file);
                    Document doc = Jsoup.parse(new File(htmlPath), "GBK"); // 自己定
                    Elements elements = doc.select("table").select("tbody"); //读取所有的tbody标签,视情况而定
                    elements.forEach(e -> {
                        //读取td中所有的span标签,视情况而定,可能有图片,自己处理
                        e.select("td").select("span").eachText().stream().filter(d -> d != null && d.trim().length() > 0).forEach(System.out::println);

                    });


                } catch (IOException e) {
                    throw new ParseWordException(Code.FILE_CONVERT_FAILURE.getCode(), Code.FILE_CONVERT_FAILURE.getMessage());
                }
            }


        }
        catch (IOException e) {
            throw new ParseWordException(Code.PARSE_FAILURE.getCode(), Code.PARSE_FAILURE.getMessage());
        }
        return null;
    }

    /**
     * parse HTML
     *
     * @param readerFile
     * @return
     * @throws IOException
     */
    private String parseHtml(File readerFile) throws IOException {
        String tempPath = "d:\\1.html"; // 创建一个零时文件,自己换一下路径

        File outFile = new File(tempPath);
        if (outFile.exists()) {
            outFile.delete(); // 删掉之前已经存在的文件
        }
        FileInputStream fis = new FileInputStream(readerFile);
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = fis.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);

        }

        return tempPath;
    }

    public static void main(String[] args) throws IOException, ParseWordException {
       ParseWorld parse = new ParseWorld();
       parse.parseWord("D:\\aaa.doc");


//
    }

    private boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖
  • 代码片段
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档