java解析Excel中图片

Posted 天河一粟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java解析Excel中图片相关的知识,希望对你有一定的参考价值。

多余话不说,直接上代码

package org.springblade.common.utils;

import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 读取Excel中图片
 *
 * @author sky.jin
 * @date 2022/6/18 10:57
 */
public class ExcelImageConverter 

	static String path = "C:\\\\Users\\\\Administrator\\\\Downloads\\\\1\\\\";

	public static void main(String[] args) throws Exception 
		String filePath = "C:\\\\Users\\\\Administrator\\\\Downloads\\\\Image1.xlsx";
		// 1、获取图片流
		Map<String, PictureData> image = getImage(filePath);
		// 2、图片写到本地
		printImg(image);
	

	/**
	 * 图片写到本地
	 * @param picList
	 */
	public static void printImg(Map<String, PictureData> picList) 
		picList.forEach((key, pic) -> 
			// 文件扩展名
			String ext = pic.suggestFileExtension();
			// 图片数据
			byte[] data = pic.getData();
			//图片保存路径
			String filePath = path + key + "." + ext;
			System.out.println(filePath);
			try (FileOutputStream out = new FileOutputStream(filePath);)
				out.write(data);
			 catch (Exception e) 
				e.printStackTrace();
			
		);
	

	/**
	 * 获取Excel中图片和位置
	 *
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static Map<String, PictureData> getImage(String filePath) throws IOException 
		if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx")) 
			throw new RuntimeException("后缀必须是.xls或.xlsx");
		
		Workbook wookbook;
		Sheet sheet;
		try(FileInputStream fis = new FileInputStream(filePath)) 
			if (filePath.endsWith(".xls")) 
				// 2003版本的excel,用.xls结尾
				wookbook = new HSSFWorkbook(fis); // 得到工作簿
				sheet = wookbook.getSheetAt(0);
				return getImageXLS((HSSFSheet) sheet);
			 else 
				// 2007版本的excel,用.xlsx结尾
				wookbook = new XSSFWorkbook(fis);//得到工作簿
				sheet = wookbook.getSheetAt(0);
				return getImageXLSX((XSSFSheet) sheet);
			
		
	

	/**
	 * 获取图片和位置 (xls)
	 *
	 * @param sheet
	 * @return
	 */
	public static Map<String, PictureData> getImageXLS(HSSFSheet sheet) 
		AtomicInteger index = new AtomicInteger();
		Map<String, PictureData> map = new HashMap<>();
		List<HSSFShape> shapes = sheet.getDrawingPatriarch().getChildren();
		shapes.forEach(v -> 
			if (v instanceof HSSFPicture) 
				HSSFPicture picture = (HSSFPicture) v;
				HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
				PictureData data = picture.getPictureData();
				// 行号-列号-图片序号
				String key = cAnchor.getRow1() + "-" + cAnchor.getCol1() + "-" + index.getAndIncrement();
				map.put(key, data);
			
		);
		return map;
	

	/**
	 * 获取图片和位置 (xlsx)
	 *
	 * @param sheet
	 * @return
	 */
	public static Map<String, PictureData> getImageXLSX(XSSFSheet sheet) 
		AtomicInteger index = new AtomicInteger();
		Map<String, PictureData> map = new HashMap<>();
		List<POIXMLDocumentPart> documentParts = sheet.getRelations();
		documentParts.forEach(v -> 
			if (v instanceof XSSFDrawing) 
				XSSFDrawing drawing = (XSSFDrawing) v;
				List<XSSFShape> shapes = drawing.getShapes();
				shapes.forEach(s -> 
					XSSFPicture picture = (XSSFPicture) s;
					XSSFClientAnchor anchor = picture.getPreferredSize();
					CTMarker marker = anchor.getFrom();
					String key = marker.getRow() + "-" + marker.getCol() + "-" + index.getAndIncrement();
					map.put(key, picture.getPictureData());
					System.out.println(key + "-" + picture.getPictureData().getData().length/1024);
				);
			
		);
		return map;
	


以上是关于java解析Excel中图片的主要内容,如果未能解决你的问题,请参考以下文章

java解析Excel中图片

Java 上传解压zip文件,并且解析文件里面的excel和图片

java类 图片导入到excel 模糊 就是图片被盖上了一层红色

Java对Excel解析(求助)

如何用java编程实现excel表中的插入图片功能

java如何实现excel导入数据库中图片也导入