首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

应用POI向excel中插入图片

2013-01-23 
使用POI向excel中插入图片??最近的一次工作中,需要向excel中插入图片。我们的要求是图片填满固定的区域,虽

使用POI向excel中插入图片

?

?

最近的一次工作中,需要向excel中插入图片。我们的要求是图片填满固定的区域,虽然图片有可能会拉伸或变形,但是,这么做在目前看来也是工作最好的了。下面是插入图片的代码:

?

            File image = null;            byte[] bytes = IOUtils.toByteArray(new FileInputStream(image));            int pictureIdx = template.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);                        CreationHelper helper = template.getCreationHelper();            ClientAnchor anchor = helper.createClientAnchor();            anchor.setCol1(2);            anchor.setRow1(6);            anchor.setCol2(2);            anchor.setRow2(6 + 2);            int LOGO_WIDTH_IN_FUNNY_POI_FORMAT = 160;            int LOGO_HEIGHT_IN_FUNNY_POI_FORMAT = 1;            anchor.setDx1(1 * XSSFShape.EMU_PER_PIXEL);            anchor.setDy1(1 * XSSFShape.EMU_PER_PIXEL);            anchor.setDx2(LOGO_WIDTH_IN_FUNNY_POI_FORMAT * XSSFShape.EMU_PER_PIXEL);            anchor.setDy2(LOGO_HEIGHT_IN_FUNNY_POI_FORMAT * XSSFShape.EMU_PER_PIXEL);            pict = drawing.createPicture(anchor, pictureIdx);

?

通过setCol1, setRow1, setCol2, setRow2设置图片所在的区域。我们定义图片的宽是LOGO_WIDTH_IN_FUNNY_POI_FORMAT。

?

?

热点排行