POI实现Excel插入图片

本文介绍如何使用POI向Excel文档中插入多张图片,并提供了一个示例代码。文章还解释了如何调整图片尺寸以保持其原始比例,以及如何解决图片插入后导致Excel文件损坏的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POI的操作Excel时,不可避免有操作图片的处理。怎么插入图片呢?网上也有不少介绍。

下面的代码是向Excel中插入多张图片的例子:

public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
try {
// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
// 读入图片1
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("**.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);

// 读入图片2
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg1 = ImageIO.read(new File("**"));
ImageIO.write(bufferImg1, "png", byteArrayOut1);

// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("test picture");
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,
(short) 1, 1, (short) 5, 5);
anchor.setAnchorType(3);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 255, 255,
(short) 6, 6, (short) 10, 10);
anchor1.setAnchorType(3);
// 插入图片1
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
// 插入图片2
patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

fileOut = new FileOutputStream("d:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
} catch (IOException io) {
io.printStackTrace();
System.out.println("erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


这边的效果没有保持原来的倍率,有点失真了,是因为HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 5, 5); 这个构造函数的原因。

HSSFClientAnchor构造函数参数的意义如下:

* @param dx1 the x coordinate within the first cell.
* @param dy1 the y coordinate within the first cell.
* @param dx2 the x coordinate within the second cell.
* @param dy2 the y coordinate within the second cell.
* @param col1 the column (0 based) of the first cell.
* @param row1 the row (0 based) of the first cell.
* @param col2 the column (0 based) of the second cell.
* @param row2 the row (0 based) of the second cell.

其中dx1,dy1这个点是定义图片在开始cell中的起始位置。这个点是开始cell的左上为原点,相对比率来确定的。不是绝对坐标。

dx2,dy2这个点是定义图片在终了cell的终了位置。这个点是终了cell的左上为原点,相对比率来确定的。不是绝对坐标。

col1,row1是定义开始cell。

col2,row2是定义终了cell。

如果我们想要保持原始的图片大小,改一下上面代码中的下面部分就可以了。

修改前:patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

修改后:patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)).resize(1);

其中resize是放大或缩小的函数。用了这个函数后,HSSFClientAnchor构造函数中的图片显示的终了cell位置就不起作用了。


以上方法虽然实现了向excel中插入图片,但是下载下来的excel文件在打开时总是报“文件错误,数据可能丢失”这种错误,求解?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值