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;

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值