JAVA读取word内表格的图片,将几行几列写入图片名(非POI)

java 复制代码
package org.springblade;


import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.TableCell;
import com.spire.doc.TableRow;
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.HashMap;
import java.util.List;

public class GetTable {
	public static void main(String[] args)throws IOException {
		//加载Word测试文档
		Document doc = new Document();
		doc.loadFromFile("E:\\测试导入\\导入模板.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();
		HashMap<String, Object> photoMap = new HashMap<>();

		//遍历表格中的行
		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());
							//得到当前表格的行和列
							String photoName = String.format("第%s行第%s列的图片.png", i + 1, j);
							photoMap.put(photoName,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);
//		}

		for (String s : photoMap.keySet()) {
			File imageFile = new File(s);
			ImageIO.write((RenderedImage) photoMap.get(s), "PNG", imageFile);

		}

	}
}

在此之前必须引入maven

XML 复制代码
      <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>5.2.0</version>
        </dependency>
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
相关推荐
code bean14 分钟前
【CMake】为什么需要清理 CMake 缓存文件?深入理解 CMake 生成器切换机制
java·spring·缓存
selt79119 分钟前
Redisson之RedissonLock源码完全解析
android·java·javascript
RestCloud34 分钟前
智能制造的底层基建:iPaaS 如何统一 ERP、MES 与 WMS 的数据流
java·wms·erp·数据传输·ipaas·mes·集成平台
guslegend1 小时前
SpringBoot源码剖析
java
皮卡龙1 小时前
Spring MVC 接收请求参数的核心
java·spring·mvc
爱笑的眼睛111 小时前
FastAPI 路由系统深度探索:超越基础 CRUD 的高级模式与架构实践
java·人工智能·python·ai
武子康2 小时前
Java-193 Spymemcached 深入解析:线程模型、Sharding 与序列化实践全拆解
java·开发语言·redis·缓存·系统架构·memcached·guava
石像鬼₧魂石2 小时前
Termux ↔ Windows 靶机 反向连接实操命令清单
linux·windows·学习
韩凡2 小时前
HashMap的理解与结构
java·开发语言·哈希算法