Java實現(xiàn)提取Word文檔表格數(shù)據(jù)
Word文檔作為一種廣泛使用的文件格式,常常承載著豐富的表格信息,這些信息可能涉及到財務(wù)報表、項目規(guī)劃、實驗數(shù)據(jù)記錄等多方面內(nèi)容。將這些表格數(shù)據(jù)提取出來,能夠方便進行數(shù)據(jù)分析以及內(nèi)容再創(chuàng)作等場景。通過使用Java實現(xiàn)Word文檔表格數(shù)據(jù)的提取,可以確保數(shù)據(jù)處理的一致性和準確性,同時大大減少所需的時間和成本。本文將介紹如何使用Java提取Word文檔中的表格數(shù)據(jù)。
本文所使用的方法需要用到免費的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文檔表格到文本文件
我們可以使用庫中的Section.getTables()方法從Word文檔的各個節(jié)中獲取表格,然后再遍歷表格的行和列,獲取表格中的段落文本,從而實現(xiàn)Word文檔表格數(shù)據(jù)的提取。以下是操作步驟示例:
- 創(chuàng)建Document對象并從文件加載Word文檔。
- 遍歷文檔各節(jié),使用Section.getTables()訪問其中的表格。
- 遍歷每個表格的行和單元格,提取文本內(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)建一個文檔對象 Document doc = new Document(); try { // 加載一個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++) { // 獲取一個表格 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++) { // 獲取一個單元格 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將表格文本寫入文件 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ù)直接寫入到Excel工作表中,從而實現(xiàn)Word文檔表格到Excel工作表的提取。以下是操作步驟:
- 創(chuàng)建Document和Workbook對象,移除Workbook的默認工作表。
- 從Word文檔加載內(nèi)容到Document,并遍歷各節(jié)與表格。
- 每遇到一個表格,就使用Workbook.getWordksheets().add()方法添加一個新工作表。
- 遍歷表格的行和單元格,提取文本內(nèi)容。
- 使用Worksheet.getRange().get().setValue()方法將提取的文本寫入對應(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對象 Document doc = new Document(); // 創(chuàng)建Workbook對象 Workbook workbook = new Workbook(); // 刪除默認的工作表 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++) { // 獲取一個表格 Table table = section.getTables().get(j); // 為每個表格創(chuàng)建一個工作表 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++) { // 獲取一個單元格 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(); } // 將單元格的文本寫入對應(yīng)的Excel工作表的單元格中 sheet.getRange().get(k + 1, l + 1).setValue(cellText); } // 自動調(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實現(xiàn)提取Word文檔表格數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Java提取Word數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Netflix Ribbon超詳細講解
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named Client),對SpringCloud Ribbon負載均衡相關(guān)知識感興趣的朋友一起看看吧2022-10-10ElasticSearch6.2.3+head插件安裝的方法步驟
這篇文章主要介紹了ElasticSearch6.2.3+head插件安裝的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-02-02Java調(diào)用Docx4j庫玩轉(zhuǎn)Word文檔處理
在 Java 開發(fā)里處理 Word 文檔時,Docx4j 可是個超厲害的庫,它能讓咱輕松創(chuàng)建,讀取,修改和轉(zhuǎn)換 Word 文檔,下面我們就來看看具體是如何操作的吧2025-02-02詳細學習Java Cookie技術(shù)(用戶登錄、瀏覽、訪問權(quán)限)
這篇文章主要為大家詳細介紹了Java Cookie技術(shù),顯示用戶上次登錄的時間、顯示用戶最近瀏覽的若干個圖片(按比例縮放)等,感興趣的小伙伴們可以參考一下2016-08-08java階乘計算獲得結(jié)果末尾0的個數(shù)代碼實現(xiàn)
今天偶然看到一個要求,求1000~10000之間的數(shù)n的階乘并計算所得的數(shù)n!末尾有多少個0?要求: 不計算 只要得到末尾有多少個0就可以了,看下面的代碼吧2013-12-12