美文网首页
POI读取word文档

POI读取word文档

作者: 蝉鸣的雨 | 来源:发表于2018-03-25 20:17 被阅读747次

最近做了一个word文档导入的功能,但是因为项目紧急,所以做的很粗糙。好不容易周末了,就自己撸了一会代码,想把他做成一个通用的工具,以备以后用到时直接黏贴。

概述

POI 的起源

POI是apache的一个开源项目,他的起始初衷是处理基于Office Open XML标准(OOXML)和Microsoft OLE 2复合文档格式(OLE2)的各种文件格式的文档,而且对于读和写都有支持。可以说是JAVA处理OFFICE文档的首选工具了。

HWPF和XWPF

POI操作word文档的两个主要模块就是HWPFXWPF
HWPF是操作Microsoft Word 97(-2007)文件的标准API入口。它还支持对旧版Word 6和Word 95文件对有限的只读功能。
XWPF是操作Microsoft Word 2007文件的标准API入口。

读取word文档

其实,POI对于word文档的读写操作提供了许多API可用,这里只提供最简单的按段落读取文字内容的demo,对于图片读取或表格的读取,以后再更新。

maven依赖

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>24.0-jre</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
    public static <T> List<String> readWordFile(String path) {
        List<String> contextList = Lists.newArrayList();
        InputStream stream = null;
        try {
            stream = new FileInputStream(new File(path));
            if (path.endsWith(".doc")) {
                HWPFDocument document = new HWPFDocument(stream);
                WordExtractor extractor = new WordExtractor(document);
                String[] contextArray = extractor.getParagraphText();
                Arrays.asList(contextArray).forEach(context -> contextList.add(CharMatcher.whitespace().removeFrom(context)));
                extractor.close();
                document.close();
            } else if (path.endsWith(".docx")) {
                XWPFDocument document = new XWPFDocument(stream).getXWPFDocument();
                List<XWPFParagraph> paragraphList = document.getParagraphs();
                paragraphList.forEach(paragraph -> contextList.add(CharMatcher.whitespace().removeFrom(paragraph.getParagraphText())));
                document.close();
            } else {
                LOGGER.debug("此文件{}不是word文件", path);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != stream) try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
                LOGGER.debug("读取word文件失败");
            }
        }
        return contextList;
    }

相关文章

  • POI读取word文档

    最近做了一个word文档导入的功能,但是因为项目紧急,所以做的很粗糙。好不容易周末了,就自己撸了一会代码,想把他做...

  • poi 读取word文档中内容

    package com.test.document.util; import java.io.File; impo...

  • 怎么用Java操作Word文档?

    Java操作word文档 Java 操作word,对word文档进行读写时,主要用 Apache写的POI这个工具...

  • word生成及word转pdf

    生成word文档 word模板转xml ,字符串替换。复杂不可维护。(不推荐) Poi-tl 。Word模板引擎,...

  • JSP 利用Apache POI 操作 Word

    利用Apache POI 操作word, 生成简单的报告文档 先上个效果图: 到Apache下载 POI 需要的相...

  • 读取word文档

    word文档内容,如下:"啊 我看见一座山 雄伟的大山 真高啊 啊 这座山是! 真的很高!

  • Java POI导出Word文档

    本文章来源 POI导出Word文档—黑壳网 昨天晚上被壳妹,威逼利诱,做点小东西,其中就有一个POI导出Word文...

  • iOS--通过webView读取word文档

    通常情况下我们需要读取word文档,在iOS中可以通过webView来读取word文档,相当简单:1.首先在vie...

  • POI解析Word中文API

    https://yq135314.iteye.com/blog/1538182 POI操作word文档的两个主要模...

  • 如何用Java将Office文档转换为PDF

    需求场景 office文档在web端在线浏览 实现思路1 利用POI分析word文档或者xls表格,转化为HTML...

网友评论

      本文标题:POI读取word文档

      本文链接:https://www.haomeiwen.com/subject/cbdkcftx.html