package ;
import ;
import .;
import ;
import .*;
import .*;
import ;
import ;
import ;
import ;
import .;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* created by rock-ayl on 2022-2-23
* 解析excel内的图片所在单元格
*/
public class ExcelImageUtils {
private final static Logger LOG = ();
/**
* 根据文件解析excel内图片
*
* @param file 文件对象
* @param sheetIndex 第几个sheet
* @return
*/
public static Map<String, Map<Integer, PictureData>> parseSheetPic(File file, int sheetIndex) {
try {
//文件流
InputStream input = new FileInputStream(file);
//文件后缀
String fileExt = (());
//实现并返回
return parseSheetPic(input, fileExt, sheetIndex);
} catch (Exception e) {
("parseSheetPic fail , file error={}", e);
}
//默认
return new HashMap<>();
}
/**
* 根据文件流及文件类型解析excel内图片
*
* @param inputStream 文件流
* @param fileExt 文件后缀 xls,xlsx
* @param sheetIndex 第几个sheet
* @return
*/
public static Map<String, Map<Integer, PictureData>> parseSheetPic(InputStream inputStream, String fileExt, int sheetIndex) {
try {
//初始化
Workbook workbook = (inputStream);
//获取sheet
Sheet sheet = (sheetIndex);
//第一行作为key
Map<Integer, String> keyMap = new HashMap<>();
//第一行的迭代器
Iterator<Cell> iterator = (0).cellIterator();
//指针
int p = 0;
//如果存在
while (()) {
//获取值
String value = ().getStringCellValue();
//如果存在
if ((value)) {
//记录该key
(p, value);
}
//无论如何+1o
p++;
}
//根据类型解析
switch (fileExt) {
case "xls":
return parseSheetPicForXLS((HSSFWorkbook) workbook, sheetIndex, keyMap);
case "xlsx":
return parseSheetPicForXLSX((XSSFWorkbook) workbook, sheetIndex, keyMap);
}
} catch (Exception e) {
("parseSheetPic fail , inputStream error={}", e);
}
//默认
return new HashMap<>();
}
/**
* 解析XLS的图片
*
* @param workbook workbook
* @param sheetIndex 第几页sheet的图片
* @return
*/
private static Map<String, Map<Integer, PictureData>> parseSheetPicForXLS(HSSFWorkbook workbook, int sheetIndex, Map<Integer, String> keyMap) {
//初始化结果
Map<String, Map<Integer, PictureData>> result = new HashMap<>();
//获取workbook所有图片列表
List<HSSFPictureData> pictures = ();
//获取对应sheet
HSSFSheet sheet = (sheetIndex);
//如果存在图片
if (() > 0) {
//获取列表
List<HSSFShape> children = ().getChildren();
//循环
for (HSSFShape shape : children) {
//获取并强转
HSSFClientAnchor anchor = (HSSFClientAnchor) ();
//如果是
if (shape instanceof HSSFPicture) {
//强转
HSSFPicture pic = (HSSFPicture) shape;
//从workbook所有图片中找到该图片
HSSFPictureData picData = (() - 1);
//如果存在改key
if ((anchor.getCol1())) {
//获取key
String key = (anchor.getCol1());
//尝试获取mep
Map<Integer, PictureData> map = (key, new HashMap<>());
//组装
(anchor.getRow1() - 1, picData);
(key, map);
}
}
}
}
//返回
return result;
}
/**
* 解析XLSX图片
*
* @return
*/
private static Map<String, Map<Integer, PictureData>> parseSheetPicForXLSX(XSSFWorkbook workbook, int sheetIndex, Map<Integer, String> keyMap) {
//初始化结果
Map<String, Map<Integer, PictureData>> result = new HashMap<>();
//获取列表
List<POIXMLDocumentPart> relationList = (sheetIndex).getRelations();
//循环1
for (POIXMLDocumentPart dr : relationList) {
//如果是类型
if (dr instanceof XSSFDrawing) {
//强转
XSSFDrawing drawing = (XSSFDrawing) dr;
//获取列表
List<XSSFShape> shapeList = ();
//循环2
for (XSSFShape shape : shapeList) {
//强转
XSSFPicture pic = (XSSFPicture) shape;
//获取其可能的表格
CTMarker ctMarker = ().getFrom();
//如果存在改key
if ((())) {
//获取key
String key = (());
//尝试获取结果map
Map<Integer, PictureData> map = (key, new HashMap<>());
//组装
(() - 1, ());
(key, map);
}
}
}
}
//返回
return result;
}
}