赞
踩
POI的操作Excel时,不可避免有操作图片的处理。怎么插入图片呢?网上也有不少介绍。
下面的代码是向Excel中插入多张图片的例子:
这样执行后的效果如下:(完全按照HSSFClientAnchor设置的参数显示)
这边的效果没有保持原来的倍率,有点失真了,是因为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位置就不起作用了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。