POI实现Excel2003插入多张图片

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

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

public static void main(String[] args) {    
        FileOutputStream fileOut = null;    
        BufferedImage bufferImg = null;    
        BufferedImage bufferImg1 = null;    
        try {    
            // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray    
            // 读入图片1    
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();    
            bufferImg = ImageIO.read(new File("d:\\test11.jpg"));    
            ImageIO.write(bufferImg, "jpg", byteArrayOut);    
                
            // 读入图片2    
            ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();    
            bufferImg1 = ImageIO.read(new File("d:\\test22.png"));    
            ImageIO.write(bufferImg1, "png", byteArrayOut1);    
   
            // 创建一个工作薄    
            HSSFWorkbook wb = new HSSFWorkbook();    
            HSSFSheet sheet1 = wb.createSheet("test picture");    
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();    
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,    
                    (short) 1, 1, (short) 5, 5);    
            anchor.setAnchorType(3);    
            HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 255, 255,    
                    (short) 6, 6, (short) 10, 10);    
            anchor1.setAnchorType(3);    
            // 插入图片1    
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut    
                    .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));    
            // 插入图片2    
            patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1    
                    .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));    
   
            fileOut = new FileOutputStream("d:/workbook.xls");    
            // 写入excel文件    
            wb.write(fileOut);    
            fileOut.close();    
        } catch (IOException io) {    
            io.printStackTrace();    
            System.out.println("erorr : " + io.getMessage());    
        } finally {    
            if (fileOut != null) {    
                try {    
                    fileOut.close();    
                } catch (IOException e) {    
                    e.printStackTrace();    
                }    
            }    
        }    
    }    
  

这样执行后的效果如下:(完全按照HSSFClientAnchor设置的参数显示)

  POI实现Excel插入多张图片 - alpsdyk2001 - 编程学习!

这边的效果没有保持原来的倍率,有点失真了,是因为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);

修改后效果如下:

 POI实现Excel插入多张图片 - alpsdyk2001 - 编程学习!

其中resize是放大或缩小的函数。用了这个函数后,HSSFClientAnchor构造函数中的图片显示的终了cell位置就不起作用了。

原文:http://blog.163.com/alpsdyk2001@126/blog/static/5279414820099266640415/

posted @ 2017-06-14 09:51  小宁同学柠檬茶  阅读(643)  评论(0)    收藏  举报