-----------java code-------------
package com.primeton.eos.arp.excel;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.primeton.tp.core.bizservice.EOSParameter;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* @author 胡晶
* @version 1.0
* @date 2009-6-15
* @class_displayName ExpExcelUtil
*/
public class ExpExcelUtil {
private WritableWorkbook wwb = null;
private Workbook wb = null;
private WritableSheet ws = null;
private ByteArrayOutputStream outStream =new ByteArrayOutputStream();
private int startRow = 0; //拼SQL查询,写excel起始行列
private int startCol = 0;
/**
* 构造方法,创建空的excel表,默认读取第一个sheet
*
*/
public ExpExcelUtil(){
try{
wwb = Workbook.createWorkbook(outStream);
wwb.createSheet("sheet1", 1);
ws = wwb.getSheet(0);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 构造方法,根据配置excel信息初始化excel模板,默认读取第一个sheet
* @param TEMPLATEID
*/
public ExpExcelUtil(String TEMPLATEID){
EOSParameter param=new EOSParameter();
InputStream inStream = null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//查出配置excel模板
String sql = "SELECT FILEBODY FROM TEMPLATE_FILE WHERE RECORDID="+ "'"+TEMPLATEID+"'";
try {
conn=param.getDBBroker().getConnection();
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery(sql);
if (rs.next()) {
inStream = rs.getBlob("FILEBODY").getBinaryStream();
}
if ("".equals(inStream) || inStream == null) {
wwb = Workbook.createWorkbook(outStream);
wwb.createSheet("sheet1", 1);
} else {
wb = Workbook.getWorkbook(inStream);
inStream.close();
wwb = Workbook.createWorkbook(outStream, wb);
}
ws = wwb.getSheet(0);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 构造方法,根据输入流,初始化Excel,默认读取第一个sheet
* @param InputStream
*/
public ExpExcelUtil(InputStream is) {
try {
wb = Workbook.getWorkbook(is);
wwb = Workbook.createWorkbook(outStream, wb);
ws = wwb.getSheet(0);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拼SQL查询前,设置起始行列
* @param col
* @param row
*/
public void querySetStart(int col,int row){
this.startCol = col;
this.startRow = row;
}
/**
* 拼SQL查询数据导入EXCEL
* @param entityName
* @param Stinrg[] expFields
* @param whereSql
*/
public void queryWithSql(String entityName,String[] expFields,String whereSql){
EOSParameter param=new EOSParameter();
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select ";
for(int i=0;i<expFields.length;i++){
sql+=expFields[i]+",";
}
sql=sql.substring(0,sql.lastIndexOf(","));
sql+=" from "+entityName+" "+whereSql;
System.out.println(sql);
try{
conn=param.getDBBroker().getConnection();
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery(sql);
int j=0;
while(rs.next()){
for(int i=0;i<expFields.length;i++){
ws.addCell(new Label(i+startCol,j+startRow,rs.getString(expFields[i])));
}
j++;
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 查出表中所有字段名称
* return List
*/
public List getAllFields(String tableName){
EOSParameter param=new EOSParameter();
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql= "select column_name from user_tab_columns where table_name = '"+tableName+"'";
List list=new ArrayList();
try{
conn=param.getDBBroker().getConnection();
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery(sql);
while (rs.next()) {
list.add(rs.getString("column_name"));
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
/**
* 设置当前操作sheet,如果不存在,创建新的sheet
* @param sheetNum
*/
public void setSheet(int sheetNum){
if(sheetNum>=wwb.getNumberOfSheets()){
int t=wwb.getNumberOfSheets();
try {
wwb.createSheet("sheet"+t, t);
ws=wwb.getSheet(t);
} catch (Exception e) {
e.printStackTrace();
}
}else{
ws = wwb.getSheet(sheetNum);
}
}
/**
* 设置当前操作sheet名称
* @param sheetName
*/
public void setSheetName(String sheetName){
ws.setName(sheetName);
}
/**
* 创建新的sheet,并把操作定位到当前sheet
* @param sheetName
*/
public void createSheet(String sheetName){
int t=wwb.getNumberOfSheets();
try {
wwb.createSheet(sheetName, t);
ws=wwb.getSheet(t);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 返回sheet数
* @return
*/
public int getNumberOfSheets(){
return wwb.getNumberOfSheets();
}
/**
* 返回当前sheet行数
* @return int
*/
public int getRows(){
return ws.getRows();
}
/**
* 返回当前sheet列数
* @return int
*/
public int getColumns(){
return ws.getColumns();
}
/**
* 合并单元格
* @param col1
* @param row1
* @param col2
* @param row2
*/
public void mergeCells(int col1,int row1,int col2,int row2){
try {
ws.mergeCells(col1,row1,col2,row2);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置单元格大小,width、height为0则不做修改
* @param col
* @param row
* @param width
* @param height
*/
public void setCellSize(int col,int row,int width,int height){
try {
ws.setColumnView(col, width);
ws.setRowView(row, height);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除行
* @param row
*/
public void removeRow(int row){
try {
ws.removeRow(row);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除列
* @param col
*/
public void removeColumn(int col){
try {
ws.removeColumn(col);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增加Cell
* @param col
* @param row
* @param str
*/
public void addCell(int col,int row,String str){
try {
ws.addCell(new Label(col,row,str));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增加Cell
* @param cell
*/
public void addCell(Label cell){
try {
ws.addCell(cell);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增加Cell,设置字大小
* @param col
* @param row
* @param str
* @param size
*/
public void addCell(int col,int row,String str,int size){
WritableCellFormat wcf=new WritableCellFormat(new WritableFont(WritableFont.ARIAL,size));
try {
ws.addCell(new Label(col,row,str,wcf));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写Excel
*/
public void write(){
try{
wwb.write();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 返回输出
* @return ByteArrayOutputStream
*/
public ByteArrayOutputStream outExcel(){
try{
if(wb!=null){
wb.close();
}
if(wwb!=null){
wwb.close();
}
}catch(Exception e){
e.printStackTrace();
}
return outStream;
}
}
--------------jsp file-----------------
<%@page import="com.primeton.eos.arp.excel.ExpExcelUtil,java.io.ByteArrayOutputStream"%>
<%
ExpExcelUtil eeu = new ExpExcelUtil();
eeu.addCell(1,1,"This is My Cell");
eeu.write();
ByteArrayOutputStream outStream= eeu.outExcel();
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="+new String(("导出excel.xls").getBytes(),"iso8859-1"));
ServletOutputStream sos = response.getOutputStream();
outStream.writeTo(sos);
out.clear();
out = pageContext.pushBody();
%>