excel中添加图片的代码

直接上代码

 

  FileOutputStream fileOut = null;
       BufferedImage bufferImg = null;
       BufferedImage bufferImgU = null;
       BufferedImage bufferImgP = null;
       BufferedImage bufferImgI= null;
       
       try {
           // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
           // 读入图片1
           ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
           bufferImg = ImageIO.read(new File(imgPath+"/chart.png"));
           ImageIO.write(bufferImg, "png", byteArrayOut);
            
           // 读入图片2
           ByteArrayOutputStream byteArrayOutU = new ByteArrayOutputStream();
           bufferImgU = ImageIO.read(new File(imgPath+"/url.png"));
           ImageIO.write(bufferImgU, "png", byteArrayOutU);
           
           // 读入图片3
           ByteArrayOutputStream byteArrayOutI= new ByteArrayOutputStream();
           bufferImgI = ImageIO.read(new File(imgPath+"/inter.png"));
           ImageIO.write(bufferImgI, "png", byteArrayOutI);
            
           // 读入图片4
           ByteArrayOutputStream byteArrayOutP = new ByteArrayOutputStream();
           bufferImgP = ImageIO.read(new File(imgPath+"/part.png"));
           ImageIO.write(bufferImgP, "png", byteArrayOutP);

           // 创建一个工作薄
           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 anchoru = new HSSFClientAnchor(0, 0, 255, 255,
                   (short) 1, 6, (short) 5, 10);
           anchoru.setAnchorType(3);
           
           HSSFClientAnchor anchorI = new HSSFClientAnchor(0, 0, 255, 255,
                   (short) 6, 1, (short) 10, 5);
           anchorI.setAnchorType(3);
           HSSFClientAnchor anchorP = new HSSFClientAnchor(0, 0, 255, 255,
                   (short) 6, 6, (short) 10, 10);
           anchorP.setAnchorType(3);
           
           
           // 插入图片1
           patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
                   .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
           // 插入图片2
           patriarch.createPicture(anchoru, wb.addPicture(byteArrayOutU
                   .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
           // 插入图片1
           patriarch.createPicture(anchorI, wb.addPicture(byteArrayOutI
                   .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
           // 插入图片2
           patriarch.createPicture(anchorP, wb.addPicture(byteArrayOutP
                   .toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

           fileOut = new FileOutputStream(path+"/每日报表.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();
               }
           }
       }


一篇很好的介绍文章:http://blog.csdn.net/chenssy/article/details/20524563

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伏特加的滋味

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值