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ù)提取出來,能夠方便進(jìn)行數(shù)據(jù)分析以及內(nèi)容再創(chuàng)作等場景。通過使用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文檔表格到文本文件
我們可以使用庫中的Section.getTables()方法從Word文檔的各個節(jié)中獲取表格,然后再遍歷表格的行和列,獲取表格中的段落文本,從而實(shí)現(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工作表中,從而實(shí)現(xiàn)Word文檔表格到Excel工作表的提取。以下是操作步驟:
- 創(chuàng)建Document和Workbook對象,移除Workbook的默認(rèn)工作表。
- 從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();
// 刪除默認(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++) {
// 獲取一個表格
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實(shí)現(xiàn)提取Word文檔表格數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Java提取Word數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java計(jì)時(shí)新姿勢StopWatch的使用方法詳解
這篇文章主要給大家介紹了關(guān)于Java計(jì)時(shí)新姿勢StopWatch的相關(guān)資料,以及java 中使用StopWatch來計(jì)算時(shí)間差的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
JAVA Spring Boot 自動配置實(shí)現(xiàn)原理詳解
這篇文章主要介紹了詳解SpringBoot自動配置原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09
springboot讀取application.yml報(bào)錯問題及解決
這篇文章主要介紹了springboot讀取application.yml報(bào)錯問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Java連接Mysql數(shù)據(jù)庫詳細(xì)代碼實(shí)例
這篇文章主要介紹了Java連接Mysql數(shù)據(jù)庫詳細(xì)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
IntelliJ IDEA2020.2.2創(chuàng)建Servlet方法及404問題
這篇文章主要介紹了IntelliJ IDEA2020.2.2創(chuàng)建Servlet方法及404問題,這里小編使用的2020.2.2企業(yè)破解版本,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

