使用POI向Excel中插入多張图片

POI3.0以上版本才支持向Excel中插入图片

http://apache.freelamp.com/jakarta/poi/release/src/poi-src-3.0-FINAL-20070503.zip

http://apache.freelamp.com/jakarta/poi/release/bin/poi-bin-3.0-FINAL-20070503.zip

新版的API可能支持的內容更豐富,不過文件也越來越大了.


package com.emis.test;

import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.awt.image.BufferedImage;
import javax.imageio.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;

public class test {

    public static void main(String[] args) {
            FileOutputStream fileOut = null;
            BufferedImage bufferImg =null;
            BufferedImage bufferImg1 = null;
            try{

          //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
          bufferImg = ImageIO.read(new File("D:\\wwwroot\\smepos3.0\\images\\link01.jpg"));
          bufferImg1 = ImageIO.read(new File("D:\\wwwroot\\smepos3.0\\images\\link02.jpg"));
          ImageIO.write(bufferImg,"jpg",byteArrayOut);
          ImageIO.write(bufferImg1,"jpg",byteArrayOut1);

        //创建一个工作薄
       HSSFWorkbook wb = new HSSFWorkbook();
       HSSFSheet sheet1 = wb.createSheet("new sheet");
       //HSSFRow row = sheet1.createRow(2);
       HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
       HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,100,50,(short) 1,1,(short)10,20);
       HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,100,50,(short) 2,30,(short)10,60);
       anchor1.setAnchorType(2);
       //插入图片
       patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
       patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));

           fileOut = new FileOutputStream("d:/workbook.xls");
           //写入excel文件
           wb.write(fileOut);
           fileOut.close();

            }catch(IOException io){
                    io.printStackTrace();
                    System.out.println("io erorr : "+ io.getMessage());
            } finally
            {
               if (fileOut != null)
               {

                   try {
                              fileOut.close();
                         }
                   catch (IOException e)
                   {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                     }
               }
            }
    }
}

转载于:https://my.oschina.net/dddgggaaa/blog/204932

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值