POI 读取BLOB类型图片并写入到Excel中

package com.foxconn;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;   
import java.io.File;   
import java.io.ByteArrayOutputStream;   
import java.io.IOException;   
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
  
import java.awt.image.BufferedImage;   
import javax.imageio.*;   

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

public class AaE {
 public static void main(String[] args) {   
        FileOutputStream fileOut = null;   
        BufferedImage bufferImg = null;   
        BufferedImage bufferImg1 = null;   
        try {   
         ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); 
      //String ReturnReason = (String) model.getParam("ReturnReason");
      // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray   
            // 读入图片1
      //BufferedImage bufferImg = ImageIO.read(new File("d:\\album-show_04.jpg"));   
            //如果是从数据库中读取的BLOB类型的图片 从 model中取得该图片 方法略       
         Blob blob = (Blob) model.getParam("fileContent");
       BufferedImage buffer_Img = imgChangeBuffer(blob);  
            ImageIO.write(bufferImg, "jpg", byteArrayOut);   
               
  
            // 创建一个工作薄   
            HSSFWorkbook wb = new HSSFWorkbook();   
            HSSFSheet sheet1 = wb.createSheet("test picture");
            HSSFRow row = sheet1.createRow(0);
            //设置默认高与宽
          sheet1.setDefaultRowHeightInPoints(10);
          sheet1.setDefaultColumnWidth((short) 20);
            HSSFCell cell = row.createCell((short)0);
            cell.setCellValue("aaa");
            HSSFCell cell2 = row.createCell((short)1);
            cell2.setCellValue("bbb");
            HSSFCell cell3 = row.createCell((short)2);
            cell3.setCellValue("ccc");
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
            //控制图片显示
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,   
                    (short) 2, 4, (short)2, 2);   
            anchor.setAnchorType(3); 
            // 插入图片
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut   
                    .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));   
          
            fileOut = new FileOutputStream("d:/aa.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();   
                }   
            }   
        }   
    }   
  public static BufferedImage imgChangeBuffer(Blob blob) throws IOException{
     byte[] data = null;
     try {
   InputStream inStream = blob.getBinaryStream();
   long nLen = blob.length();
          int nSize = (int) nLen;
          data = new byte[nSize];
          inStream.read(data);
          inStream.close();
  } catch (SQLException e) {
    this.setMessage("获取图片数据失败,原因:"+e.getMessage());
  }
  BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));
     return bis;

    }

}

你可能感兴趣的:(学习笔记)