實(shí)戰(zhàn) | Java讀取Word,包含表格!
本文轉(zhuǎn)載自微信公眾號(hào)「JAVA日知錄」,作者單一色調(diào)。轉(zhuǎn)載本文請(qǐng)聯(lián)系JAVA日知錄公眾號(hào)。
不能每天都發(fā)雞湯呀,今天分享一篇開發(fā)實(shí)戰(zhàn)。
業(yè)務(wù)需求
我們有這樣一個(gè)需求,需要抽取出WORD文檔中的內(nèi)容,然后組裝成特定的json格式發(fā)送給第三方引擎接口,輸入?yún)f(xié)議如下:
- {
- "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日知錄"
- }
- ]
- }
這個(gè)輸入格式一看就是需要我們分段落和表格讀取word中的內(nèi)容,既然需求已定,那就直接開始動(dòng)手寫代碼吧。
基于POI實(shí)現(xiàn)
把 “java如何讀取word” 拿到百度去搜索,答案基本都是利用POI來(lái)實(shí)現(xiàn)。當(dāng)然利用POI確實(shí)可以實(shí)現(xiàn)按段落和表格提取出內(nèi)容并組裝成上述格式,但是在實(shí)踐過程中有下面2個(gè)問題:
需要分別處理兩種格式docx、docPOI使用不同的API來(lái)讀取docx和doc,所以讀取邏輯我們需要編寫兩次。
POI讀取doc的段落時(shí)會(huì)把表格的內(nèi)容也讀取出來(lái) 這個(gè)問題比較坑,poi有單獨(dú)的方法讀取文檔中所有表格,但是在讀取doc格式段落文檔的時(shí)候會(huì)把表格內(nèi)容也讀取出來(lái),所以我們需要用如下方法排除掉表格:
- //讀取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);
- //排除表格內(nèi)容
- if (!para.isInTable()) {
- System.out.println(para.text());
- }
- }
考慮以上兩種原因,我們最后并沒有采取POI來(lái)實(shí)現(xiàn)word內(nèi)容提取功能,而是采用第二種方法,即利用 Spire.Doc for Java 來(lái)實(shí)現(xiàn)。
Spire.Doc for Java
Spire.Doc for Java 是一款專業(yè)的 Java Word 組件,開發(fā)人員使用它可以輕松地將 Word 文檔創(chuàng)建、讀取、編輯、轉(zhuǎn)換和打印等功能集成到自己的 Java 應(yīng)用程序中。
作為一款完全獨(dú)立的組件,Spire.Doc for Java 的運(yùn)行環(huán)境無(wú)需安裝 Microsoft Office。官網(wǎng)地址是 https://www.e-iceblue.cn/,我們項(xiàng)目中使用的開源免費(fèi)版。
首先我們修改maven倉(cāng)庫(kù)地址
- <repositories>
- <repository>
- <id>com.e-iceblue</id>
- <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
- </repository>
- </repositories>
引入對(duì)應(yīng)的jar包
- <dependency>
- <groupId>e-iceblue</groupId>
- <artifactId>spire.doc.free</artifactId>
- <version>3.9.0</version>
- </dependency>
讀取word,這里展示的是測(cè)試類
- 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中的內(nèi)容,而后根據(jù)系統(tǒng)業(yè)務(wù)要求的格式進(jìn)行封裝即可。