這篇文章主要介紹了如何用Java讀取Word表格中文本和圖片的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇如何用Java讀取Word表格中文本和圖片文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
創(chuàng)新互聯(lián)主營(yíng)海城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開(kāi)發(fā),海城h5重慶小程序開(kāi)發(fā)搭建,海城網(wǎng)站營(yíng)銷推廣歡迎海城等地區(qū)企業(yè)咨詢
代碼編譯工具:IntelliJ IDEA
Jdk版本:1.8.0
測(cè)試文檔:Word .docx 2013
Jar包:free spire.doc.jar 3.9.0
用于測(cè)試的Word文檔如下:
打開(kāi)Project Structure(Shift+Ctrl+Alt+S)界面,選擇【Modules】—【Dependencies】,點(diǎn)擊“+”,【JARs or directories…】,選擇本地路徑中的jar包,添加后,勾選,點(diǎn)擊“OK”或者“Apply”導(dǎo)入jar。
需在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>
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文件(用于寫入表格中提取的文本) 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); } } }
完成代碼編輯后,執(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é)果:
關(guān)于“如何用Java讀取Word表格中文本和圖片”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“如何用Java讀取Word表格中文本和圖片”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)標(biāo)題:如何用Java讀取Word表格中文本和圖片
瀏覽地址:http://aaarwkj.com/article12/gghhdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、外貿(mào)建站、建站公司、品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營(yíng)銷推廣、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)