java poi读取数据库中的图片,再把图片放入excel中

package com.ufun;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.imageio.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;

import com.mysql.jdbc.Blob;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class Main {

 static String dbURL = "jdbc:mysql://218.213.98.132:1433/texas_cn_log";
 static String userID = "texas";
 static String passwd = "texas123";

 public static void main(String args[]) {

  try {
   Class.forName("com.mysql.jdbc.Driver");
   Connection conn = (Connection) DriverManager.getConnection(dbURL,
     userID, passwd);
   // conn.setAutoCommit(false);

   
   
   String sql2 = "SELECT id,uid,content,image ,submitTime,readStatus from feedback limit 200,300 ";
   PreparedStatement stmt2 = (PreparedStatement) conn
     .prepareStatement(sql2);
   ResultSet rs = (ResultSet) stmt2.executeQuery();

   int i = 0;
   
   OutputStream outf = new FileOutputStream("E:\\feedback200-300.xls");
   HSSFWorkbook wb = new HSSFWorkbook();
   HSSFSheet sheet = wb.createSheet("feedback数据");
   //只能创造一个这样的对象
   HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
   while (rs.next()) {
    i++;
    HSSFRow row = sheet.createRow(rs.getRow() - 1);
    for (int c = 0; c <= rs.getMetaData().getColumnCount(); c++) {
     HSSFCell cell = row.createCell(c);
     if (c < rs.getMetaData().getColumnCount()) {
      if (c != 3) {
       cell.setCellValue(rs.getString(c + 1));
      } else {
       // InputStream is = rs.getBinaryStream(3);

       if (null != rs.getBlob("image") && !rs.getBlob("image").equals(null)) {
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        
        Blob image = (Blob) rs.getBlob("image");
        BufferedImage bufferImg = imgChangeBuffer(image);

        // bufferImg = ImageIO.read(is);
        ImageIO.write(bufferImg, "jpg", byteArrayOut);

        // int dx1, int dy1, int dx2, int dy2, short
        // col1, int row1, short col2, int row2)
        // dx1
        // dy1
        // dx2
        // dy2
        // col1 图片的左上角放在第几个列cell
        // row1图片的左上角放在第几个行cell
        // col2 图片的右下角放在第几个列cell
        // row2图片的右下角放在第几个行cell

        HSSFClientAnchor anchor = new HSSFClientAnchor(
          0, 0, 255, 255, (short) c, i-1,
          (short) c, i-1);
        patriarch.createPicture(anchor, wb.addPicture(
          byteArrayOut.toByteArray(),
          HSSFWorkbook.PICTURE_TYPE_JPEG));
       }

      }
     }

    }

    /*
     * String id = resultSet.getString("id");
     * System.out.println(id); String uid =
     * resultSet.getString("uid"); String content =
     * resultSet.getString("content"); String image =
     * resultSet.getString("image"); //String submitTime =
     * resultSet.getString("submitTime"); //String readStatus =
     * resultSet.getString("readStatus"); File image2 = new
     * File("D:"+File.separator+id+".jpg"); FileOutputStream fos =
     * new FileOutputStream(image2); byte[] buffer = new
     * byte[100000]; InputStream is = resultSet.getBinaryStream(3);
     * while (is.read(buffer) > 0) { fos.write(buffer); }
     * fos.close();
     */
   //}
   //wb.write(outf);
   //outf.close();
   conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * @param blob
  * @return
  * @throws IOException
  */
 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) {
   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、付费专栏及课程。

余额充值