com.whaleal.icefrog.poi.excel.ExcelPicUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of icefrog-poi Show documentation
Show all versions of icefrog-poi Show documentation
icefrog POI工具类(对MS Office操作)
The newest version!
package com.whaleal.icefrog.poi.excel;
import com.whaleal.icefrog.core.collection.CollectionUtil;
import com.whaleal.icefrog.core.lang.Precondition;
import com.whaleal.icefrog.core.util.StrUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Excel图片工具类
*
* @author Looly
* @author wh
* @since 1.0.0
*/
public class ExcelPicUtil {
/**
* 获取工作簿指定sheet中图片列表
*
* @param workbook 工作簿{@link Workbook}
* @param sheetIndex sheet的索引
* @return 图片映射,键格式:行_列,值:{@link PictureData}
*/
public static Map getPicMap( Workbook workbook, int sheetIndex ) {
Precondition.notNull(workbook, "Workbook must be not null !");
if (sheetIndex < 0) {
sheetIndex = 0;
}
if (workbook instanceof HSSFWorkbook) {
return getPicMapXls((HSSFWorkbook) workbook, sheetIndex);
} else if (workbook instanceof XSSFWorkbook) {
return getPicMapXlsx((XSSFWorkbook) workbook, sheetIndex);
} else {
throw new IllegalArgumentException(StrUtil.format("Workbook type [{}] is not supported!", workbook.getClass()));
}
}
// -------------------------------------------------------------------------------------------------------------- Private method start
/**
* 获取XLS工作簿指定sheet中图片列表
*
* @param workbook 工作簿{@link Workbook}
* @param sheetIndex sheet的索引
* @return 图片映射,键格式:行_列,值:{@link PictureData}
*/
private static Map getPicMapXls( HSSFWorkbook workbook, int sheetIndex ) {
final Map picMap = new HashMap<>();
final List pictures = workbook.getAllPictures();
if (CollectionUtil.isNotEmpty(pictures)) {
final HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
HSSFClientAnchor anchor;
int pictureIndex;
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
if (shape instanceof HSSFPicture) {
pictureIndex = ((HSSFPicture) shape).getPictureIndex() - 1;
anchor = (HSSFClientAnchor) shape.getAnchor();
picMap.put(StrUtil.format("{}_{}", anchor.getRow1(), anchor.getCol1()), pictures.get(pictureIndex));
}
}
}
return picMap;
}
/**
* 获取XLSX工作簿指定sheet中图片列表
*
* @param workbook 工作簿{@link Workbook}
* @param sheetIndex sheet的索引
* @return 图片映射,键格式:行_列,值:{@link PictureData}
*/
private static Map getPicMapXlsx( XSSFWorkbook workbook, int sheetIndex ) {
final Map sheetIndexPicMap = new HashMap<>();
final XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
XSSFDrawing drawing;
for (POIXMLDocumentPart dr : sheet.getRelations()) {
if (dr instanceof XSSFDrawing) {
drawing = (XSSFDrawing) dr;
final List shapes = drawing.getShapes();
XSSFPicture pic;
CTMarker ctMarker;
for (XSSFShape shape : shapes) {
if (shape instanceof XSSFPicture) {
pic = (XSSFPicture) shape;
ctMarker = pic.getPreferredSize().getFrom();
sheetIndexPicMap.put(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData());
}
// 其他类似于图表等忽略,see: https://github.com/loolly/icefrog/issues/I38857
}
}
}
return sheetIndexPicMap;
}
// -------------------------------------------------------------------------------------------------------------- Private method end
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy