POI实现Excel插入多张图片(转)

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

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

  1. public static void main(String[] args) {   
  2.          FileOutputStream fileOut = null;   
  3.          BufferedImage bufferImg = null;   
  4.          BufferedImage bufferImg1 = null;   
  5.         try {   
  6.             // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray   
  7.             // 读入图片1   
  8.              ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
  9.              bufferImg = ImageIO.read(new File("d:\\test11.jpg"));   
  10.              ImageIO.write(bufferImg, "jpg", byteArrayOut);   
  11.                
  12.             // 读入图片2   
  13.              ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();   
  14.              bufferImg1 = ImageIO.read(new File("d:\\test22.png"));   
  15.              ImageIO.write(bufferImg1, "png", byteArrayOut1);   
  16.   
  17.             // 创建一个工作薄   
  18.              HSSFWorkbook wb = new HSSFWorkbook();   
  19.              HSSFSheet sheet1 = wb.createSheet("test picture");   
  20.              HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
  21.              HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,   
  22.                      (short) 1, 1, (short) 5, 5);   
  23.              anchor.setAnchorType(3);   
  24.              HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 255, 255,   
  25.                      (short) 6, 6, (short) 10, 10);   
  26.              anchor1.setAnchorType(3);   
  27.             // 插入图片1   
  28.              patriarch.createPicture(anchor, wb.addPicture(byteArrayOut   
  29.                      .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));   
  30.             // 插入图片2   
  31.              patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1   
  32.                      .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));   
  33.   
  34.              fileOut = new FileOutputStream("d:/workbook.xls");   
  35.             // 写入excel文件   
  36.              wb.write(fileOut);   
  37.              fileOut.close();   
  38.          } catch (IOException io) {   
  39.              io.printStackTrace();   
  40.              System.out.println("erorr : " + io.getMessage());   
  41.          } finally {   
  42.             if (fileOut != null) {   
  43.                 try {   
  44.                      fileOut.close();   
  45.                  } catch (IOException e) {   
  46.                      e.printStackTrace();   
  47.                  }   
  48.              }   
  49.          }   
  50.      }   
  51.   

这样执行后的效果如下:(完全按照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位置就不起作用了

猜你喜欢

转载自zzc1684.iteye.com/blog/1846180