溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何用Java讀取Word表格中文本和圖片

發(fā)布時間:2023-05-04 11:48:28 來源:億速云 閱讀:160 作者:zzz 欄目:編程語言

這篇文章主要介紹了如何用Java讀取Word表格中文本和圖片的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇如何用Java讀取Word表格中文本和圖片文章都會有所收獲,下面我們一起來看看吧。

1. 程序環(huán)境準備

  • 代碼編譯工具:IntelliJ IDEA

  • Jdk版本:1.8.0

  • 測試文檔:Word .docx 2013

  • Jar包:free spire.doc.jar 3.9.0

用于測試的Word文檔如下:

如何用Java讀取Word表格中文本和圖片

Jar導(dǎo)入步驟及方法:

方法1:手動導(dǎo)入。

打開Project Structure(Shift+Ctrl+Alt+S)界面,選擇【Modules】—【Dependencies】,點擊“+”,【JARs or directories…】,選擇本地路徑中的jar包,添加后,勾選,點擊“OK”或者“Apply”導(dǎo)入jar。

如何用Java讀取Word表格中文本和圖片

方法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);

        //獲取第一個表格
        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)容

                    //遍歷段落中的所有子對象
                    for (int x = 0; x < paragraph.getChildObjects().getCount(); x++)
                    {
                        Object object = paragraph.getChildObjects().get(x);
                        //判定對象是否為圖片
                        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項目文件夾路徑,如:

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é)果:

如何用Java讀取Word表格中文本和圖片

圖片讀取結(jié)果:

如何用Java讀取Word表格中文本和圖片

關(guān)于“如何用Java讀取Word表格中文本和圖片”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“如何用Java讀取Word表格中文本和圖片”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI