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;
}
}