Java實(shí)現(xiàn)提取Word文檔表格數(shù)據(jù)
Word文檔作為一種廣泛使用的文件格式,常常承載著豐富的表格信息,這些信息可能涉及到財(cái)務(wù)報(bào)表、項(xiàng)目規(guī)劃、實(shí)驗(yàn)數(shù)據(jù)記錄等多方面內(nèi)容。將這些表格數(shù)據(jù)提取出來(lái),能夠方便進(jìn)行數(shù)據(jù)分析以及內(nèi)容再創(chuàng)作等場(chǎng)景。通過(guò)使用Java實(shí)現(xiàn)Word文檔表格數(shù)據(jù)的提取,可以確保數(shù)據(jù)處理的一致性和準(zhǔn)確性,同時(shí)大大減少所需的時(shí)間和成本。本文將介紹如何使用Java提取Word文檔中的表格數(shù)據(jù)。
本文所使用的方法需要用到免費(fèi)的Free Spire.Doc for Java,Maven:
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.3.2</version> </dependency>
用Java提取Word文檔表格到文本文件
我們可以使用庫(kù)中的Section.getTables()方法從Word文檔的各個(gè)節(jié)中獲取表格,然后再遍歷表格的行和列,獲取表格中的段落文本,從而實(shí)現(xiàn)Word文檔表格數(shù)據(jù)的提取。以下是操作步驟示例:
- 創(chuàng)建Document對(duì)象并從文件加載Word文檔。
- 遍歷文檔各節(jié),使用Section.getTables()訪問(wèn)其中的表格。
- 遍歷每個(gè)表格的行和單元格,提取文本內(nèi)容。
- 將提取的文本添加到StringBuilder。
- 輸出或保存StringBuilder中的內(nèi)容。
代碼示例:
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import java.io.FileWriter; import java.io.IOException; public class ExtractWordTable { public static void main(String[] args) { // 創(chuàng)建一個(gè)文檔對(duì)象 Document doc = new Document(); try { // 加載一個(gè)Word文檔 doc.loadFromFile("GSample.docx"); // 遍歷文檔中的各節(jié) for (int i = 0; i < doc.getSections().getCount(); i++) { // 獲取一節(jié) Section section = doc.getSections().get(i); // 遍歷該節(jié)中的表格 for (int j = 0; j < section.getTables().getCount(); j++) { // 獲取一個(gè)表格 Table table = section.getTables().get(j); // 收集所有表格內(nèi)容 StringBuilder tableText = new StringBuilder(); for (int k = 0; k < table.getRows().getCount(); k++) { // 獲取一行 TableRow row = table.getRows().get(k); // 遍歷行中的單元格 StringBuilder rowText = new StringBuilder(); for (int l = 0; l < row.getCells().getCount(); l++) { // 獲取一個(gè)單元格 TableCell cell = row.getCells().get(l); // 遍歷段落以獲取單元格中的文本 String cellText = ""; for (int m = 0; m < cell.getParagraphs().getCount(); m++) { Paragraph paragraph = cell.getParagraphs().get(m); cellText += paragraph.getText() + " "; } if (l < row.getCells().getCount() - 1) { rowText.append(cellText).append("\t"); } else { rowText.append(cellText).append("\n"); } } tableText.append(rowText); } // 使用try-with-resources將表格文本寫(xiě)入文件 try (FileWriter writer = new FileWriter("output/Tables/Section-" + (i + 1) + "-Table-" + (j + 1) + ".txt")) { writer.write(tableText.toString()); } } } } catch (IOException e) { throw new RuntimeException(e); } } }
結(jié)果
用Java提取Word文檔表格到Excel文件
我們還可以將提取數(shù)據(jù)的方法與Free Spire.XLS for Java結(jié)合,將提取到的表格數(shù)據(jù)直接寫(xiě)入到Excel工作表中,從而實(shí)現(xiàn)Word文檔表格到Excel工作表的提取。以下是操作步驟:
- 創(chuàng)建Document和Workbook對(duì)象,移除Workbook的默認(rèn)工作表。
- 從Word文檔加載內(nèi)容到Document,并遍歷各節(jié)與表格。
- 每遇到一個(gè)表格,就使用Workbook.getWordksheets().add()方法添加一個(gè)新工作表。
- 遍歷表格的行和單元格,提取文本內(nèi)容。
- 使用Worksheet.getRange().get().setValue()方法將提取的文本寫(xiě)入對(duì)應(yīng)工作表的單元格,并設(shè)置格式。
- 保存Workbook為Excel文件。
代碼示例
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.xls.FileFormat; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class ExtractWordTableToExcel { public static void main(String[] args) { // 創(chuàng)建Document對(duì)象 Document doc = new Document(); // 創(chuàng)建Workbook對(duì)象 Workbook workbook = new Workbook(); // 刪除默認(rèn)的工作表 workbook.getWorksheets().clear(); try { // 加載Word文檔 doc.loadFromFile("Sample.docx"); // 遍歷文檔中的各節(jié) for (int i = 0; i < doc.getSections().getCount(); i++) { // 獲取一節(jié) Section section = doc.getSections().get(i); // 遍歷該節(jié)中的表格 for (int j = 0; j < section.getTables().getCount(); j++) { // 獲取一個(gè)表格 Table table = section.getTables().get(j); // 為每個(gè)表格創(chuàng)建一個(gè)工作表 Worksheet sheet = workbook.getWorksheets().add("Section-" + (i + 1) + "-Table-" + (j + 1)); for (int k = 0; k < table.getRows().getCount(); k++) { // 獲取一行 TableRow row = table.getRows().get(k); for (int l = 0; l < row.getCells().getCount(); l++) { // 獲取一個(gè)單元格 TableCell cell = row.getCells().get(l); // 遍歷單元格中的段落以獲取文本 String cellText = ""; for (int m = 0; m < cell.getParagraphs().getCount(); m++) { Paragraph paragraph = cell.getParagraphs().get(m); if (m > 0 && m < cell.getParagraphs().getCount() - 1) { cellText += paragraph.getText() + "\n"; } else { cellText += paragraph.getText(); } // 將單元格的文本寫(xiě)入對(duì)應(yīng)的Excel工作表的單元格中 sheet.getRange().get(k + 1, l + 1).setValue(cellText); } // 自動(dòng)調(diào)整列寬 sheet.autoFitColumn(l + 1); } } } } } catch (Exception e) { throw new RuntimeException(e); } // 保存為Excel文件 workbook.saveToFile("output/WordTableToExcel.xlsx", FileFormat.Version2016); // 釋放資源 workbook.dispose(); } }
結(jié)果
到此這篇關(guān)于Java實(shí)現(xiàn)提取Word文檔表格數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Java提取Word數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Netflix Ribbon超詳細(xì)講解
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶(hù)端的負(fù)載均衡器,Ribbon具有一系列帶有名稱(chēng)的客戶(hù)端(Named Client),對(duì)SpringCloud Ribbon負(fù)載均衡相關(guān)知識(shí)感興趣的朋友一起看看吧2022-10-10Spring Security添加二次認(rèn)證的項(xiàng)目實(shí)踐
在用戶(hù)自動(dòng)登錄后,可以通過(guò)對(duì)密碼進(jìn)行二次校驗(yàn)進(jìn)而確保用戶(hù)的真實(shí)性,本文就來(lái)介紹一下Spring Security添加二次認(rèn)證的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Mybatis 級(jí)聯(lián)刪除的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis 級(jí)聯(lián)刪除的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11ElasticSearch6.2.3+head插件安裝的方法步驟
這篇文章主要介紹了ElasticSearch6.2.3+head插件安裝的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Java調(diào)用Docx4j庫(kù)玩轉(zhuǎn)Word文檔處理
在 Java 開(kāi)發(fā)里處理 Word 文檔時(shí),Docx4j 可是個(gè)超厲害的庫(kù),它能讓咱輕松創(chuàng)建,讀取,修改和轉(zhuǎn)換 Word 文檔,下面我們就來(lái)看看具體是如何操作的吧2025-02-02詳細(xì)學(xué)習(xí)Java Cookie技術(shù)(用戶(hù)登錄、瀏覽、訪問(wèn)權(quán)限)
這篇文章主要為大家詳細(xì)介紹了Java Cookie技術(shù),顯示用戶(hù)上次登錄的時(shí)間、顯示用戶(hù)最近瀏覽的若干個(gè)圖片(按比例縮放)等,感興趣的小伙伴們可以參考一下2016-08-08java階乘計(jì)算獲得結(jié)果末尾0的個(gè)數(shù)代碼實(shí)現(xiàn)
今天偶然看到一個(gè)要求,求1000~10000之間的數(shù)n的階乘并計(jì)算所得的數(shù)n!末尾有多少個(gè)0?要求: 不計(jì)算 只要得到末尾有多少個(gè)0就可以了,看下面的代碼吧2013-12-12