通过JXL或POI在Excel中插入图像

时间:2013-03-12 16:57:37

标签: java apache-poi jxl

我被困在将图像插入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();

1 个答案:

答案 0 :(得分:0)

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

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

此代码将图片放在由角1,2和角7,10限定的矩形中。我不确定是否可以添加双击它的功能。