实战 | Java读取Word,包含表格!

开发 后端
Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中。

[[387140]]

本文转载自微信公众号「JAVA日知录」,作者单一色调。转载本文请联系JAVA日知录公众号。

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

业务需求

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

  1.     "tables": [ 
  2.         { 
  3.             "cells": [ 
  4.                 { 
  5.                     "col": 1, 
  6.                     "row_span": 1, 
  7.                     "row": 1, 
  8.                     "col_span": 1, 
  9.                     "content""车辆名称" 
  10.                 } 
  11.             ], 
  12.             "id": 0, 
  13.             "row_num": 2 
  14.         } 
  15.     ], 
  16.     "paragraps": [ 
  17.         { 
  18.             "para_id": 1, 
  19.             "content""Hello,JAVA日知录" 
  20.         } 
  21.     ] 

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

基于POI实现

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

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

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

  1. //读取doc 
  2. HWPFDocument doc = new HWPFDocument(stream); 
  3. Range range = doc.getRange(); 
  4.  
  5. //读取段落 
  6. int num = range.numParagraphs(); 
  7. Paragraph para; 
  8. for (int i=0; i<num; i++) { 
  9.     para = range.getParagraph(i); 
  10.     //排除表格内容 
  11.     if (!para.isInTable()) { 
  12.         System.out.println(para.text()); 
  13.     } 

考虑以上两种原因,我们最后并没有采取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仓库地址

  1. <repositories> 
  2.     <repository> 
  3.         <id>com.e-iceblue</id> 
  4.         <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> 
  5.     </repository> 
  6. </repositories> 

 

 

引入对应的jar包

  1. <dependency> 
  2.     <groupId>e-iceblue</groupId> 
  3.     <artifactId>spire.doc.free</artifactId> 
  4.     <version>3.9.0</version> 
  5. </dependency> 

 

读取word,这里展示的是测试类

  1. public class SpireApplication { 
  2.  
  3.     public static void main(String[] args) { 
  4.         String path = "D:\\testDoc22.doc"
  5.         spireParaghDoc(path); 
  6.         spireForTableOfDoc(path);  
  7.     } 
  8.  
  9.     //读取段落 
  10.     public static void spireParaghDoc(String path) { 
  11.         Document doc = new Document(path); 
  12.         for (int i = 0; i < doc.getSections().getCount(); i++) { 
  13.             Section section = doc.getSections().get(i); 
  14.             for (int j = 0; j < section.getParagraphs().getCount(); j++) { 
  15.                 Paragraph paragraph = section.getParagraphs().get(j); 
  16.                 System.out.println(paragraph.getText()); 
  17.             } 
  18.         } 
  19.     } 
  20.  
  21.     //读取表格 
  22.     public static void spireForTableOfDoc(String path) { 
  23.         Document doc = new Document(path); 
  24.         for (int i = 0; i < doc.getSections().getCount(); i++) { 
  25.             Section section = doc.getSections().get(i); 
  26.             for (int j = 0; j < section.getBody().getChildObjects().getCount(); j++) { 
  27.                 DocumentObject obj = section.getBody().getChildObjects().get(j); 
  28.                 if (obj.getDocumentObjectType() == DocumentObjectType.Table) { 
  29.                     Table table = (Table) obj; 
  30.                     for (int k = 0; k < table.getRows().getCount(); k++) { 
  31.                         TableRow rows = table.getRows().get(k); 
  32.                         for (int p = 0; p < rows.getCells().getCount(); p++) { 
  33.                             for (int h = 0; h < rows.getCells().get(p).getParagraphs().getCount(); h++) { 
  34.                                 Paragraph f = rows.getCells().get(p).getParagraphs().get(h); 
  35.                                 System.out.println(f.getText()); 
  36.                             } 
  37.                         } 
  38.                     } 
  39.                 } 
  40.             } 
  41.         } 
  42.     } 
  43.  

 

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

 

责任编辑:武晓燕 来源: JAVA日知录
相关推荐

2021-12-14 07:40:08

Excel自动化办公

2021-12-28 09:24:49

Python邮件Word

2009-08-19 10:46:48

C#操作Word表格

2009-08-19 10:42:08

C#操作Word表格

2009-09-01 11:25:08

C#读取Word文件

2009-08-28 17:34:14

读取word文档

2009-09-01 11:21:02

C#读取word内容

2009-09-01 13:10:39

C#读取Word

2009-08-28 17:46:18

C#读取Word文档

2020-08-19 17:14:26

Python数据函数

2020-04-13 13:50:15

Python电子表格编程语言

2022-07-11 12:14:56

Pandashtmljson

2021-05-16 07:08:18

ExcelWord技巧

2012-01-17 14:09:54

JavaSwing

2020-11-06 08:28:44

Python

2016-01-15 10:01:12

PHPOCR实战图像

2010-05-28 16:04:03

读取MySQL数据库

2011-04-02 09:30:46

JavaIO

2011-04-15 14:37:45

JavaCsv

2015-10-09 15:58:42

Java读取大文件
点赞
收藏

51CTO技术栈公众号