自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)戰(zhàn) | Java讀取Word,包含表格!

開發(fā) 后端
Spire.Doc for Java 是一款專業(yè)的 Java Word 組件,開發(fā)人員使用它可以輕松地將 Word 文檔創(chuàng)建、讀取、編輯、轉(zhuǎn)換和打印等功能集成到自己的 Java 應(yīng)用程序中。

[[387140]]

本文轉(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é)議如下:

  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.     ] 

這個(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),所以我們需要用如下方法排除掉表格:

  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.     //排除表格內(nèi)容 
  11.     if (!para.isInTable()) { 
  12.         System.out.println(para.text()); 
  13.     } 

考慮以上兩種原因,我們最后并沒有采取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ù)地址

  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> 

 

 

引入對(duì)應(yīng)的jar包

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

 

讀取word,這里展示的是測(cè)試類

  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中的內(nèi)容,而后根據(jù)系統(tǒng)業(yè)務(wù)要求的格式進(jìn)行封裝即可。

 

責(zé)任編輯:武曉燕 來(lái)源: JAVA日知錄
相關(guān)推薦

2021-12-14 07:40:08

Excel自動(dòng)化辦公

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-08-28 17:34:14

讀取word文檔

2009-09-01 11:21:02

C#讀取word內(nèi)容

2009-09-01 11:25:08

C#讀取Word文件

2009-09-01 13:10:39

C#讀取Word

2020-08-19 17:14:26

Python數(shù)據(jù)函數(shù)

2009-08-28 17:46:18

C#讀取Word文檔

2025-02-12 00:35:24

WinForm框架工具

2021-05-16 07:08:18

ExcelWord技巧

2022-07-11 12:14:56

Pandashtmljson

2020-04-13 13:50:15

Python電子表格編程語(yǔ)言

2012-01-17 14:09:54

JavaSwing

2025-01-15 13:46:23

2020-11-06 08:28:44

Python

2016-01-15 10:01:12

PHPOCR實(shí)戰(zhàn)圖像

2010-05-28 16:04:03

讀取MySQL數(shù)據(jù)庫(kù)

2011-04-15 14:37:45

JavaCsv
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)