標籤:style blog http io color ar os 使用 sp
1.java匯出excel報表:
package cn.jcenterhome.util;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExportExcel {
public final static String exportExcel(List<String> header, List<Map<String, Object>> list,String xlsName,String type,HttpServletResponse response) {
String result="系統提示:Excel檔案匯出成功!";
// 以下開始輸出到EXCEL
try {
//定義輸出資料流,以便開啟儲存對話方塊______________________begin
response.reset();// 清空輸出資料流
response.setHeader("Content-disposition", "attachment; filename="+ new String(xlsName.getBytes("GB2312"),"ISO8859-1"));
// 設定輸出檔案頭
response.setContentType("application/octet-stream");// 定義輸出類型
OutputStream os = response.getOutputStream();// 取得輸出資料流
//定義輸出資料流,以便開啟儲存對話方塊_______________________end
/** **********建立活頁簿************ */
WritableWorkbook workbook = Workbook.createWorkbook(os);
/** ************設定儲存格字型************** */
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD);
/** ************以下設定三種儲存格樣式,靈活備用************ */
// 用於標題置中
WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊
wcf_center.setAlignment(Alignment.CENTRE); // 文字水平對齊
wcf_center.setWrap(false); // 文字是否換行
// 用於本文居左
WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 線條
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對齊
wcf_left.setAlignment(Alignment.LEFT); // 文字水平對齊
wcf_left.setWrap(false); // 文字是否換行
WritableSheet [] sheets = null;
if(list!=null&&list.size()>0){
/** **********建立工作表************ */
int scount = list.size()/65535 + ((list.size()%65535)>0?1:0);
sheets = new WritableSheet[scount];
for(int i=0;i<scount;i++){
WritableSheet sheet = workbook.createSheet("Sheet"+i+1, i);
/** **********設定縱橫列印(預設為縱打)、列印紙***************** */
jxl.SheetSettings sheetset = sheet.getSettings();
sheetset.setProtected(false);
sheets[i]=sheet;
}
/** ***************以下是EXCEL開頭大標題,暫時省略********************* */
//sheet.mergeCells(0, 0, colWidth, 0);
//sheet.addCell(new Label(0, 0, "XX報表", wcf_center));
/** ***************以下是EXCEL第一行欄位標題********************* */
for(int j=0;j<scount;j++){
for(int i=0;i<header.size();i++){
sheets[j].addCell(new Label(i, 0,header.get(i),wcf_center));
}
}
}else{
sheets = new WritableSheet[header.size()];
WritableSheet sheet = workbook.createSheet("Sheet"+1, 0);
/** **********設定縱橫列印(預設為縱打)、列印紙***************** */
jxl.SheetSettings sheetset = sheet.getSettings();
sheetset.setProtected(false);
sheets[0]=sheet;
for(int i=0;i<header.size();i++){
sheets[0].addCell(new Label(i, 0,header.get(i),wcf_center));
}
}
/** ***************以下是EXCEL本文資料********************* */
if(list!=null){
for (int i = 0; i < list.size(); i++)
{
Map<String, Object> value=list.get(i);
int s = ((i+1)/65535)+(((i+1)%65535)>0?1:0)-1;
System.out.println("export i position is :"+i+";s value is :"+s);
int j = i%65535;
WritableSheet sheet = sheets[s];
if(type.equals("exportAllOrder")){
sheet.addCell(new Label(0, j+1,value.get("username")==null?"":value.get("username").toString(),wcf_left));
sheet.addCell(new Label(1, j+1,value.get("orderNo")==null?"":value.get("orderNo").toString(),wcf_left));
sheet.addCell(new Label(2, j+1,value.get("ordertime")==null?"":value.get("ordertime").toString(),wcf_left));
sheet.addCell(new Label(3, j+1,value.get("ordermoney")==null?"":value.get("ordermoney").toString(),wcf_left));
sheet.addCell(new Label(4, j+1,value.get("realMoneyS")==null?"":value.get("realMoneyS").toString(),wcf_left));
String orderstatu=value.get("orderstatu")==null?"":value.get("orderstatu").toString();
String orderstatus="";
if(orderstatu.equals("0")){
orderstatus="未支付";
}else if(orderstatu.equals("1")){
orderstatus="已支付";
}else if(orderstatu.equals("-1")){
orderstatus="已取消";
}else if(orderstatu.equals("-2")){
orderstatus="已失效";
}
sheet.addCell(new Label(5, j+1,orderstatus,wcf_left));
sheet.addCell(new Label(6, j+1,value.get("paymentway")==null?"":value.get("paymentway").toString(),wcf_left));
}
}
}
/** **********將以上緩衝中的內容寫到EXCEL檔案中******** */
workbook.write();
/** *********關閉檔案************* */
workbook.close();
} catch (Exception e) {
result="系統提示:Excel檔案匯出失敗,原因:"+ e.toString();
// System.out.println(result);
e.printStackTrace();
}finally{
if(list!=null)list.clear();
}
return result;
}
2.調用匯出方法
List<String> header=new ArrayList<String>();
header.add("使用者名稱");
header.add("訂單號");
header.add("訂單時間");
header.add("訂單金額");
header.add("實際金額");
header.add("訂單狀態");
header.add("支付方式");
ExportExcel.exportExcel(header,list,"訂單表.xls","exportAllOrder",response);
return null;
3.jsp頁面,點擊匯出
<a href="訪問後台匯出的路徑(訪問2)">匯出</a>
4.所使用jar包 jxl-2.6.jar
:http://files.cnblogs.com/chiyj/excel%E5%AF%BC%E5%87%BAjar.rar
java匯出excel報表