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.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
public static void main(String[] args) throws IOException
String filePath = "C:\\\\Users\\\\Administrator\\\\Downloads\\\\Image.xlsx";
Map<String, PictureData> image = getImage(filePath);
System.out.println(image);
/**
* 获取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());
);
);
return map;
以上是关于java解析Excel中图片的主要内容,如果未能解决你的问题,请参考以下文章
Java 上传解压zip文件,并且解析文件里面的excel和图片