如何用Java读取Word表格中文本和图片

发布时间:2023-05-04 11:48:28 作者:zzz
来源:亿速云 阅读:141

这篇文章主要介绍了如何用Java读取Word表格中文本和图片的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用Java读取Word表格中文本和图片文章都会有所收获,下面我们一起来看看吧。

1. 程序环境准备

用于测试的Word文档如下:

如何用Java读取Word表格中文本和图片

Jar导入步骤及方法:

方法1:手动导入。

打开Project Structure(Shift+Ctrl+Alt+S)界面,选择【Modules】—【Dependencies】,点击“+”,【JARs or directories…】,选择本地路径中的jar包,添加后,勾选,点击“OK”或者“Apply”导入jar。

如何用Java读取Word表格中文本和图片

方法2:Maven仓库导入。

需在pom.xml文件中配置maven路径并指定free spire.doc.jar 3.9.0的依赖,然后下载导入。具体配置如下:

<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");

        //获取第一节
        Section section = doc.getSections().get(0);

        //获取第一个表格
        ITable table = section.getTables().get(0);

        //创建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);

        //创建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");//获取文本内容

                    //遍历段落中的所有子对象
                    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");//写入内容到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. 文本、图片读取效果

完成代码编辑后,执行程序,读取表格中的文本数据和图片。代码中的文件路径为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

在代码中,文件路径可自定义为其他路径。

文本数据读取结果:

如何用Java读取Word表格中文本和图片

图片读取结果:

如何用Java读取Word表格中文本和图片

关于“如何用Java读取Word表格中文本和图片”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“如何用Java读取Word表格中文本和图片”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 项目中MySQL数据源转SQL server数据源Dual无效
  2. 记一次Oracle释放表空间,还原数据实操

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java word

上一篇:java空接口的功能怎么应用

下一篇:Matlab中自带的Java操作是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》