日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實戰(zhàn)|Java讀取Word,包含表格!

本文轉(zhuǎn)載自微信公眾號「JAVA日知錄」,作者單一色調(diào)。轉(zhuǎn)載本文請聯(lián)系JAVA日知錄公眾號。

我們提供的服務有:成都網(wǎng)站制作、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、高州ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的高州網(wǎng)站制作公司

不能每天都發(fā)雞湯呀,今天分享一篇開發(fā)實戰(zhàn)。

業(yè)務需求

我們有這樣一個需求,需要抽取出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.     ] 

這個輸入格式一看就是需要我們分段落和表格讀取word中的內(nèi)容,既然需求已定,那就直接開始動手寫代碼吧。

基于POI實現(xiàn)

把 “java如何讀取word” 拿到百度去搜索,答案基本都是利用POI來實現(xiàn)。當然利用POI確實可以實現(xiàn)按段落和表格提取出內(nèi)容并組裝成上述格式,但是在實踐過程中有下面2個問題:

需要分別處理兩種格式docx、docPOI使用不同的API來讀取docx和doc,所以讀取邏輯我們需要編寫兩次。

POI讀取doc的段落時會把表格的內(nèi)容也讀取出來 這個問題比較坑,poi有單獨的方法讀取文檔中所有表格,但是在讀取doc格式段落文檔的時候會把表格內(nè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
  9.     para = range.getParagraph(i); 
  10.     //排除表格內(nèi)容 
  11.     if (!para.isInTable()) { 
  12.         System.out.println(para.text()); 
  13.     } 

考慮以上兩種原因,我們最后并沒有采取POI來實現(xiàn)word內(nèi)容提取功能,而是采用第二種方法,即利用 Spire.Doc for Java 來實現(xiàn)。

Spire.Doc for Java

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

作為一款完全獨立的組件,Spire.Doc for Java 的運行環(huán)境無需安裝 Microsoft Office。官網(wǎng)地址是 https://www.e-iceblue.cn/,我們項目中使用的開源免費版。

首先我們修改maven倉庫地址

 
 
 
  1.  
  2.      
  3.         com.e-iceblue 
  4.         http://repo.e-iceblue.com/nexus/content/groups/public/ 
  5.      
  6.  

 

引入對應的jar包

 
 
 
  1.  
  2.     e-iceblue 
  3.     spire.doc.free 
  4.     3.9.0 
  5.  

 

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


本文題目:實戰(zhàn)|Java讀取Word,包含表格!
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/djpgepp.html