利用Java讀取Word表格中文本和圖片的方法實(shí)例
本文通過Java程序來展示如何讀取Word表格,包括讀取表格中的文本和圖片。下面是具體實(shí)現(xiàn)的步驟和方法。
1. 程序環(huán)境準(zhǔn)備
- 代碼編譯工具:IntelliJ IDEA
- Jdk版本:1.8.0
- 測試文檔:Word .docx 2013
- Jar包:free spire.doc.jar 3.9.0
用于測試的Word文檔如下:

Jar導(dǎo)入步驟及方法:
方法1:手動(dòng)導(dǎo)入。
打開Project Structure(Shift+Ctrl+Alt+S)界面,選擇【Modules】—【Dependencies】,點(diǎn)擊“+”,【JARs or directories…】,選擇本地路徑中的jar包,添加后,勾選,點(diǎn)擊“OK”或者“Apply”導(dǎo)入jar。

方法2:Maven倉庫導(dǎo)入。
需在pom.xml文件中配置maven路徑并指定free spire.doc.jar 3.9.0的依賴,然后下載導(dǎo)入。具體配置如下:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>free.spire.doc</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
2. Java代碼
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.interfaces.ITable;
import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class GetTable {
public static void main(String[] args)throws IOException {
//加載Word測試文檔
Document doc = new Document();
doc.loadFromFile("inputfile.docx");
//獲取第一節(jié)
Section section = doc.getSections().get(0);
//獲取第一個(gè)表格
ITable table = section.getTables().get(0);
//創(chuàng)建txt文件(用于寫入表格中提取的文本)
String output = "ReadTextFromTable.txt";
File textfile = new File(output);
if (textfile.exists())
{
textfile.delete();
}
textfile.createNewFile();
FileWriter fw = new FileWriter(textfile, true);
BufferedWriter bw = new BufferedWriter(fw);
//創(chuàng)建List
List images = new ArrayList();
//遍歷表格中的行
for (int i = 0; i < table.getRows().getCount(); i++)
{
TableRow row = table.getRows().get(i);
//遍歷每行中的單元格
for (int j = 0; j < row.getCells().getCount(); j++)
{
TableCell cell = row.getCells().get(j);
//遍歷單元格中的段落
for (int k = 0; k < cell.getParagraphs().getCount(); k++)
{
Paragraph paragraph = cell.getParagraphs().get(k);
bw.write(paragraph.getText() + "\t");//獲取文本內(nèi)容
//遍歷段落中的所有子對(duì)象
for (int x = 0; x < paragraph.getChildObjects().getCount(); x++)
{
Object object = paragraph.getChildObjects().get(x);
//判定對(duì)象是否為圖片
if (object instanceof DocPicture)
{
//獲取圖片
DocPicture picture = (DocPicture) object;
images.add(picture.getImage());
}
}
}
}
bw.write("\r\n");//寫入內(nèi)容到txt文件
}
bw.flush();
bw.close();
fw.close();
//將圖片以PNG文件格式保存
for (int z = 0; z < images.size(); z++)
{
File imagefile = new File(String.format("提取的表格圖片-%d.png", z));
ImageIO.write((RenderedImage) images.get(z), "PNG", imagefile);
}
}
}
3. 文本、圖片讀取效果
完成代碼編輯后,執(zhí)行程序,讀取表格中的文本數(shù)據(jù)和圖片。代碼中的文件路徑為IDEA項(xiàng)目文件夾路徑,如:
C:\Users\Administrator\IdeaProjects\Table_Doc\ReadTextFromTable.txt
C:\Users\Administrator\IdeaProjects\Table_Doc\提取的表格圖片-0.png
C:\Users\Administrator\IdeaProjects\Table_Doc\inputfile.docx
在代碼中,文件路徑可自定義為其他路徑。
文本數(shù)據(jù)讀取結(jié)果:

圖片讀取結(jié)果:

總結(jié)
到此這篇關(guān)于利用Java讀取Word表格中文本和圖片的文章就介紹到這了,更多相關(guān)Java讀取Word文本和圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程
這篇文章主要介紹了Spring Boot非Web項(xiàng)目運(yùn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
如何將maven項(xiàng)目導(dǎo)出jar包(最簡單方法)
大家都知道對(duì)于將maven項(xiàng)目導(dǎo)出jar包有好幾種方式,本文給大家分享一種方式最容易且最方便,感興趣的朋友跟隨小編一起看看吧2023-11-11
Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺(tái)項(xiàng)目 附源碼
這篇文章主要介紹了Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺(tái)項(xiàng)目附源碼,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
springboot調(diào)用HTML文件注意事項(xiàng)及說明
這篇文章主要介紹了springboot調(diào)用HTML文件注意事項(xiàng)及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
使用Spring?AOP實(shí)現(xiàn)用戶操作日志功能
這篇文章主要介紹了使用Spring?AOP實(shí)現(xiàn)了用戶操作日志功能,功能實(shí)現(xiàn)需要一張記錄日志的log表,結(jié)合示例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Java數(shù)據(jù)結(jié)構(gòu)之樹和二叉樹的相關(guān)資料
這篇文章主要介紹了Java?數(shù)據(jù)結(jié)構(gòu)之樹和二叉樹相關(guān)資料,文中通過示例代碼和一些相關(guān)題目來做介紹,非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下!2023-01-01

