POI 操作EXCEL 之插入图片 2003,2007

需要用的包 是   poi-ooxml
下面是下载地址
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22poi-ooxml%22

直接上代码 ,要说的全在注释里了 。
package com.excel;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.imageio.ImageIO;

//Excel 2003
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//Excel 2007
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcel {
	
	public static void main(String[] args) throws IOException {
	    //要插Excel中的图片
	    String picPath="http://pic.nipic.com/2007-11-09/2007119122712983_2.jpg";
	    XSSFWorkbook xWb = new XSSFWorkbook();  
	    TestExcel.insertImage(xWb,picPath);
	    TestExcel.insertImage(picPath);
	    FileOutputStream fileOut = null;  
	    // 输出到磁盘  
        fileOut = new FileOutputStream("F:/1/excel2007x"  
                + System.currentTimeMillis() + ".xlsx");  
        xWb.write(fileOut); 
        fileOut.close();  
        System.out.println("well Done ! ");
	}
	
	

    //操作 2003版excel
    public static void insertImage(String picPath){
        FileOutputStream fileOut = null;     
        BufferedImage bufferImg = null;      
        try {                 
            //也可以读取一个本地现有的Excel文件 版本要对应哦 
            //HSSFWorkbook wb=new HSSFWorkbook(new FileInputStream(new File("F:/16.jpg")));
            HSSFWorkbook wb=new HSSFWorkbook();
            // 创建一个工作薄     
            HSSFSheet sheet1 = wb.createSheet("test picture");     
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();     
            for(int i=0;i<2;i++){
                
              //new一个URL对象  
                URL url = new URL(picPath);  
                //打开链接  
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
                //设置请求方式为"GET"  
                conn.setRequestMethod("GET");  
                //超时响应时间为5秒  
                conn.setConnectTimeout(5 * 1000);  
                //通过输入流获取图片数据  
                InputStream inStream = conn.getInputStream();  
                // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray                
                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
                // 读入图片   可以是输入流 也可以试 本地 File对象
                bufferImg = ImageIO.read(inStream);    
                //bufferImg = ImageIO.read(new File("F:/16.jpg"));  
                ImageIO.write(bufferImg, "jpg", byteArrayOut);
                //前四位 是 指定位置的两个单元格 中的偏移量,  横向最大 1023 纵向最大 255,单位不明,大概是一个比例
                HSSFClientAnchor anchor = new HSSFClientAnchor(500, 120, 1023, 255,     
                        (short) 0, 0+i*30, (short) 10, 20+i*20);
                anchor.setAnchorType(3);
                patriarch.createPicture(anchor, wb.addPicture(byteArrayOut     
                        .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                
                byteArrayOut.close();
                inStream.close();
                conn.disconnect();
            }         
            fileOut = new FileOutputStream("F:/1/excel2003h"  
                    + System.currentTimeMillis() + ".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();     
                }     
            }     
        }     
        
        
    }
  
	
	
	 //操作 2007版 及以上版本 excel
    public static void insertImage(XSSFWorkbook wb,String picPath) {  
       
        BufferedImage bufferImg = null;  
        try {  
            
          //new一个URL对象  
            URL url = new URL(picPath);  
            //打开链接  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            //设置请求方式为"GET"  
            conn.setRequestMethod("GET");  
            //超时响应时间为5秒  
            conn.setConnectTimeout(5 * 1000);  
            //通过输入流获取图片数据   
            InputStream inStream = conn.getInputStream();  
            // 读取图片  
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
            
            //也可以使用文件对象  各取所需
            bufferImg = ImageIO.read(inStream);
//            bufferImg = ImageIO.read(new File("F:/16.jpg"));  
           
            //如果是 png、gif 就设对应值 我只试过 jpg png 是OK的, 如果设的和传入的图片不符,会有色差,可以自行尝试
            ImageIO.write(bufferImg, "jpg", byteArrayOut);  
            
            // 创建excel   Sheet ,这个是必须的,不然图片插到哪呢
            XSSFSheet sheet=wb.createSheet("sheet0");
            
            // 设置图片位置  这个对象 对应 2003版 中HSSFPatriarch 用法 类似
            XSSFDrawing patriarch = sheet.createDrawingPatriarch();  
            //偏移量  这个有点恶心, 这个单位直接以万 计, 10000以下 基本设了等于没设。原因不明 ,操作2003 的 HSSF 是正常的比例
            XSSFClientAnchor anchor = new XSSFClientAnchor(50*10000, 0, 100, 100,  
                     2, 10,  12, 25);  
            
            //这个参数还不太清楚 有 0、2、3 三个值,貌似和图片与单元格的相对位置有关 ,设不设都不影响图片位置
            //anchor.setAnchorType(2);
            
          //如果是 png、gif 就设对应值
            patriarch  
                    .createPicture(anchor, wb.addPicture(  
                            byteArrayOut.toByteArray(),  
                            XSSFWorkbook.PICTURE_TYPE_JPEG));  
            byteArrayOut.close();
            inStream.close();
            conn.disconnect();
        } catch (FileNotFoundException e) {  
            System.out.println(e.getLocalizedMessage());  
  
        } catch (IOException e) {  
            System.out.println(e.getLocalizedMessage());  
  
        }  
    }  
  
      
  

}


猜你喜欢

转载自silencelyn.iteye.com/blog/2170520