利用Java讀取Word表格中文本和圖片的方法實(shí)例
本文通過(guò)Java程序來(lái)展示如何讀取Word表格,包括讀取表格中的文本和圖片。下面是具體實(shí)現(xiàn)的步驟和方法。
1. 程序環(huán)境準(zhǔn)備
- 代碼編譯工具:IntelliJ IDEA
- Jdk版本:1.8.0
- 測(cè)試文檔:Word .docx 2013
- Jar包:free spire.doc.jar 3.9.0
用于測(cè)試的Word文檔如下:
Jar導(dǎo)入步驟及方法:
方法1:手動(dòng)導(dǎo)入。
打開(kāi)Project Structure(Shift+Ctrl+Alt+S)界面,選擇【Modules】—【Dependencies】,點(diǎn)擊“+”,【JARs or directories…】,選擇本地路徑中的jar包,添加后,勾選,點(diǎn)擊“OK”或者“Apply”導(dǎo)入jar。
方法2:Maven倉(cāng)庫(kù)導(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測(cè)試文檔 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文件(用于寫(xiě)入表格中提取的文本) 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");//寫(xiě)入內(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)文章
java異步編程CompletableFuture使用示例詳解
這篇文章主要為大家介紹了java異步編程CompletableFuture使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11如何使用Spring-Test對(duì)Spring框架進(jìn)行單元測(cè)試
這篇文章主要介紹了如何使用Spring-Test對(duì)Spring框架進(jìn)行單元測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring Boot2開(kāi)發(fā)之Spring Boot整合Shiro兩種詳細(xì)方法
這篇文章主要介紹了Spring Boot2開(kāi)發(fā)之Spring Boot整合Shiro詳細(xì)方法,需要的朋友可以參考下2020-03-03深入解析Java編程中的StringBuffer與StringBuider
這篇文章主要介紹了Java編程中的StringBuffer與StringBuider,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09這一次搞懂Spring自定義標(biāo)簽以及注解解析原理說(shuō)明
這篇文章主要介紹了這一次搞懂Spring自定義標(biāo)簽以及注解解析原理說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08