0

我被困在将图像插入到 excel 文件中。条件是我不希望显示全尺寸图像。图像大小是常规像素 (1280 * 1024),但在 excel 中我想显示大约 10%。如果有人双击图像,则应显示完整尺寸的图像。当我们离开单元格时,它应该再次具有相同的大小(10%)。任何帮助将非常感激。

Workbook wb = new HSSFWorkbook();
   Sheet sheet = wb.createSheet("My Sample Excel");

   InputStream inputStream = new FileInputStream("C:/opt/ZZEclipseWorkspace2/WFCLAuto/ScreenShots/12_Mar_2013__09_40_22PM_192.168.30.145.jpeg");

   byte[] bytes = IOUtils.toByteArray(inputStream);

   int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);


   inputStream.close();


   CreationHelper helper = wb.getCreationHelper();

   Drawing drawing = sheet.createDrawingPatriarch();
   ClientAnchor anchor = helper.createClientAnchor();
   //set top-left corner for the image
   anchor.setCol1(1);
   anchor.setRow1(2);

   Picture pict = drawing.createPicture(anchor, pictureIdx);
   pict.resize(0.1);

   FileOutputStream fileOut = null;
   fileOut = new FileOutputStream("C:/opt/ZZEclipseWorkspace2/WFCLAuto/ScreenShots/testing1.xls");
   wb.write(fileOut);
   fileOut.close();
4

1 回答 1

0

尝试设置左上角和右下角,使其仅填充您想要的部分。

HSSFClientAnchor anchor;
anchor=new HSSFClientAnchor(0,0,0,255,(short)1,2,(short)7,10);
anchor.setAnchorType(2);

此代码将把图片放在一个以角 1,2 和角 7,10 为界的矩形中。不过,我不确定是否要添加双击它的功能。

于 2013-03-13T14:22:30.637 回答