4

实战 | JAVA读取WORD,包含表格。

 3 years ago
source link: https://my.oschina.net/u/1388595/blog/4984169
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

不能每天都发鸡汤呀,今天分享一篇开发实战。

我们有这样一个需求,需要抽取出WORD文档中的内容,然后组装成特定的json格式发送给第三方引擎接口,输入协议如下:

{
    "tables": [
        {
            "cells": [
                {
                    "col": 1,
                    "row_span": 1,
                    "row": 1,
                    "col_span": 1,
                    "content": "车辆名称"
                }
            ],
            "id": 0,
            "row_num": 2
        }
    ],
    "paragraps": [
        {
            "para_id": 1,
            "content": "Hello,JAVA日知录"
        }
    ]
}

这个输入格式一看就是需要我们分段落和表格读取word中的内容,既然需求已定,那就直接开始动手写代码吧。

基于POI实现

把 “java如何读取word” 拿到百度去搜索,答案基本都是利用POI来实现。当然利用POI确实可以实现按段落和表格提取出内容并组装成上述格式,但是在实践过程中有下面2个问题:

  • 需要分别处理两种格式 docxdocPOI使用不同的API来读取docx和doc,所以读取逻辑我们需要编写两次。

  • POI读取doc的段落时会把表格的内容也读取出来 这个问题比较坑,poi有单独的方法读取文档中所有表格,但是在读取doc格式段落文档的时候会把表格内容也读取出来,所以我们需要用如下方法排除掉表格:

//读取doc
HWPFDocument doc = new HWPFDocument(stream);
Range range = doc.getRange();

//读取段落
int num = range.numParagraphs();
Paragraph para;
for (int i=0; i<num; i++) {
    para = range.getParagraph(i);
    //排除表格内容
    if (!para.isInTable()) {
        System.out.println(para.text());
    }
}

考虑以上两种原因,我们最后并没有采取POI来实现word内容提取功能,而是采用第二种方法,即利用 Spire.Doc for Java 来实现。

Spire.Doc for Java

Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中。
作为一款完全独立的组件,Spire.Doc for Java 的运行环境无需安装 Microsoft Office。官网地址是 https://www.e-iceblue.cn/,我们项目中使用的开源免费版。

  • 首先我们修改maven仓库地址

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
  • 引入对应的jar包

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>3.9.0</version>
</dependency>
  • 读取word,这里展示的是测试类

public class SpireApplication {

    public static void main(String[] args) {
        String path = "D:\\testDoc22.doc";
        spireParaghDoc(path);
        spireForTableOfDoc(path); 
    }

    //读取段落
    public static void spireParaghDoc(String path) {
        Document doc = new Document(path);
        for (int i = 0; i < doc.getSections().getCount(); i++) {
            Section section = doc.getSections().get(i);
            for (int j = 0; j < section.getParagraphs().getCount(); j++) {
                Paragraph paragraph = section.getParagraphs().get(j);
                System.out.println(paragraph.getText());
            }
        }
    }

    //读取表格
    public static void spireForTableOfDoc(String path) {
        Document doc = new Document(path);
        for (int i = 0; i < doc.getSections().getCount(); i++) {
            Section section = doc.getSections().get(i);
            for (int j = 0; j < section.getBody().getChildObjects().getCount(); j++) {
                DocumentObject obj = section.getBody().getChildObjects().get(j);
                if (obj.getDocumentObjectType() == DocumentObjectType.Table) {
                    Table table = (Table) obj;
                    for (int k = 0; k < table.getRows().getCount(); k++) {
                        TableRow rows = table.getRows().get(k);
                        for (int p = 0; p < rows.getCells().getCount(); p++) {
                            for (int h = 0; h < rows.getCells().get(p).getParagraphs().getCount(); h++) {
                                Paragraph f = rows.getCells().get(p).getParagraphs().get(h);
                                System.out.println(f.getText());
                            }
                        }
                    }
                }
            }
        }
    }

}

通过上面代码我们就可以按段落和表格读取WORD中的内容,而后根据系统业务要求的格式进行封装即可。

ba0e825f-cdc4-44c8-bcfb-75255d26ce05.jpg

本文分享自微信公众号 - JAVA日知录(javadaily)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK