概述
业务中经常会遇到在单元格内填充图片的需求,而且要求指定图片在单元格内的位置。
一般都是用的apache的poi
,设置图片坐标。
HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)
dx1 dy1 起始单元格中的x,y坐标.
dx2 dy2 结束单元格中的x,y坐标
col1,row1 指定起始的单元格,下标从0开始
col2,row2 指定结束的单元格 ,下标从0开始
demo
public static void main(String[] args) {
byte[] imgBytes = ResourceUtil.readBytes("test.jpg");
// 创建一个 ExcelWriter
ExcelWriter writer = ExcelUtil.getWriter("output.xlsx");
// 获取 sheet 对象
Sheet sheet = writer.getWorkbook().getSheetAt(0);
int rowNum = 0; // 假设图片插入在第一行
int colNum = 0; // 假设图片插入在第一列
// 在指定的单元格内插入图片
insertImagesToCell(sheet, rowNum, colNum, imgBytes);
// 保存到文件
writer.flush();
}
private static void insertImagesToCell(Sheet sheet, int rowNum, int colNum, byte[] imageBytes) {
// 获取 Excel 的工作簿并添加图片
Workbook workbook = sheet.getWorkbook();
// 获取 Drawing 对象
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing == null) {
drawing = sheet.createDrawingPatriarch();
}
// 创建 Excel 的行和列
Row row = sheet.getRow(rowNum);
if (row == null) {
row = sheet.createRow(rowNum);
}
//创建单元格
Cell cell = row.createCell(colNum);
int pictureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);
// 创建图片的定位点
XSSFClientAnchor anchor = new XSSFClientAnchor();
//图片起始列
anchor.setCol1(colNum);
//图片起始行
anchor.setRow1(rowNum);
//图片结束列
anchor.setCol2(colNum + 1);
//图片结束行
anchor.setRow2(rowNum + 1);
//图片左上角在开始单元格中的X坐标;>0向右;<0向左(有点类似html页面中的padding)
anchor.setDx1(Units.EMU_PER_PIXEL * 100);
//图片左上角在开始单元格中的Y坐标;>0向下;<0向上(有点类似html页面中的padding)
anchor.setDy1(Units.EMU_PER_PIXEL *(100));
//图片右下角在结束单元格中的X坐标;>0向右;<0向左
anchor.setDx2(Units.EMU_PER_PIXEL *(-100));
//图片右下角在结束单元格中的Y坐标;>0向下;<0向上
anchor.setDy2(Units.EMU_PER_PIXEL *(-100));
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
// 创建并设置图片
drawing.createPicture(anchor, pictureIdx);
}
效果图
填充多张图片demo
填充多张图片,且开始单元格和结束单元格为同一个单元格
public static void main(String[] args) {
byte[] imgBytes = ResourceUtil.readBytes("test.jpg");
// 创建一个 ExcelWriter
ExcelWriter writer = ExcelUtil.getWriter("output.xlsx");
// 获取 sheet 对象
Sheet sheet = writer.getWorkbook().getSheetAt(0);
int rowNum = 0; // 假设图片插入在第一行
int colNum = 0; // 假设图片插入在第一列
// 在指定的单元格内插入图片
insertImagesToCell(sheet, rowNum, colNum, imgBytes);
// 保存到文件
writer.flush();
}
private static void insertImagesToCell(Sheet sheet, int rowNum, int colNum, byte[] imageBytes) {
// 获取 Excel 的工作簿并添加图片
Workbook workbook = sheet.getWorkbook();
// 创建 Excel 的行和列
Row row = sheet.getRow(rowNum);
if (row == null) {
row = sheet.createRow(rowNum);
}
//创建单元格
Cell cell = row.createCell(colNum);
// 边距
int padding = Units.toEMU(20);
// 计算每张图片的宽度和位置
int numImages = 5;
// 获取 Drawing 对象
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing == null) {
drawing = sheet.createDrawingPatriarch();
}
//上一张图片的左上角X坐标
int preDx1 = 0;
//上一张图片的右下角X坐标
int preDx2 = 0;
//图片宽度
int defaultImgWidth = Units.toEMU(80);
//图片高度;图片右下角相对结束单元格的y高度
int defaultImgHeight = Units.toEMU(100);
for (int i = 0; i < numImages; i++) {
int pictureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);
// 创建图片的定位点
XSSFClientAnchor anchor = new XSSFClientAnchor();
//图片起始列
anchor.setCol1(colNum);
//图片起始行
anchor.setRow1(rowNum);
//图片结束列
anchor.setCol2(colNum);
//图片结束行
anchor.setRow2(rowNum);
//上一张图片右下角的x坐标 + 图片间距
int dx1 = preDx2 + padding;
//当前图片的左上角x坐标 + 图片宽度
int dx2 = dx1 + defaultImgWidth;
//当前图片x坐标
anchor.setDx1(dx1);
//上边距
anchor.setDy1(padding);
//图片宽度
anchor.setDx2(dx2);
//图片高度
anchor.setDy2(defaultImgHeight);
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
// 创建并设置图片
drawing.createPicture(anchor, pictureIdx);
preDx1 = dx1;
preDx2 = dx2;
}
}
填充效果