当前位置:   article > 正文

java POI实现向Excel中插入图片_poi createpicture

poi createpicture

       做Web开发免不了要与Excel打交道。今天老大给我一个任务-导出Excel。开始想的还是蛮简单的,无非就是查找,构建Excel,response下载即可。但是有一点不同,就是要加入图片,就是这个加入图片搞了好久。同时网络上确实没有发现比较好的资料,所以写这篇博文记录之,供自己和博友们查询,参考。

       在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。一、获取HSSFPatriarch对象,二、new HSSFClientAnchor对象,三、调用createPicture方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:

  1. public class ExcelImageTest {
  2. public static void main(String[] args) {
  3. FileOutputStream fileOut = null;
  4. BufferedImage bufferImg = null;
  5. //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
  6. try {
  7. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  8. bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg"));
  9. ImageIO.write(bufferImg, "jpg", byteArrayOut);
  10. HSSFWorkbook wb = new HSSFWorkbook();
  11. HSSFSheet sheet1 = wb.createSheet("test picture");
  12. //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
  13. HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
  14. //anchor主要用于设置图片的属性
  15. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);
  16. anchor.setAnchorType(3);
  17. //插入图片
  18. patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
  19. fileOut = new FileOutputStream("D:/测试Excel.xls");
  20. // 写入excel文件
  21. wb.write(fileOut);
  22. System.out.println("----Excle文件已生成------");
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }finally{
  26. if(fileOut != null){
  27. try {
  28. fileOut.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. }

      如下为执行后的结果:


      至于为什么会是这样的结果,主要是因为HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)这个构造函数造成的,下面我就来解释这个构造函数:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各个参数的含义如下:

      dx1:the x coordinate within the first cell。

      dy1:the y coordinate within the first cell。

      dx2:the x coordinate within the second cell。

      dy2:the y coordinate within the second cell。

      col1:the column (0 based) of the first cell。

      row1:the row (0 based) of the first cell。

      col2:the column (0 based) of the second cell。

      row2:the row (0 based) of the second cell。

      这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。


      下面是有两个不同的构造函数所创建的,从这幅图中我们可以清晰看到上面八个参数的含义和不同之处。


      上面是插入一张图片,那么实现插入多张图片呢?其实很简单,构造多个不同的HSSFClientAnchor对象,控制好那八个参数,如下:

HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8); 
            HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16); 
            
            //插入图片  
            patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
            patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

      其余代码一样,得到如下结果:


      下篇我将提供一个Excel生成的通用模板,支持自定样式、标题、写入图片等!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/272999
推荐阅读
相关标签
  

闽ICP备14008679号