天天看點

jsp頁面将資料導出到Excel

最近做了一個功能,通過jsp頁面點選導出按鈕将查詢出來的資料導出到Excel中。

由于以前也做過類似的導出功能,但是每次做都看看以前的代碼,或者再上網查是以開發的效率非常低,是以今天花一點時間将這個導出功能整理出來。

這個導出主要使用jxl插件來實作。不多說了,直接上代碼:

一、清單頁面的部分代碼

<input type="button" class="cxBut2" value="導出資料" οnclick="_export()"/> 
           
function _export(){
	document.location.href="download.jsp?czId=<%=czId%>&czNum=<%=czNum%>&beginIssue=<%=beginIssue%>&endIssue=<%=endIssue%>&pageNum=<%=pageNum%>&czName=" target="_blank" rel="external nofollow" +$("#czId").find("option:selected").text();
}
           

二、download.jsp

<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zhcw.kaijiang.service.ExportExcal"%>
<%@page import="java.io.OutputStream"%>
<%
	String czNum = request.getParameter("czNum");
	String czId = request.getParameter("czId");
	String czName = request.getParameter("czName");
	String beginIssue = request.getParameter("beginIssue");
	String endIssue = request.getParameter("endIssue");
	String pageNum = request.getParameter("pageNum");
	response.resetBuffer();
	response.setHeader("Content-Disposition", "attachment;filename="+ new String(czName.getBytes("UTF-8"), "iso8859-1")+".xls");//指定下載下傳的檔案名
	response.setContentType("application/vnd.ms-excel");
	try{
		ExportExcal exportExcal = new ExportExcal(beginIssue, czId, czName, czNum, endIssue, pageNum);
		exportExcal.export(response.getOutputStream());
	}catch(Exception ex){
		ex.printStackTrace();
	}
%>
           

這個頁面主要負責接收上一個頁面傳來的資料,然後調用背景相應的導出方法來實作導出功能。

注意:這兩行代碼必須要寫:

response.resetBuffer();<pre name="code" class="html">response.setContentType("application/vnd.ms-excel");
           

三、背景處理類

package com.zhcw.kaijiang.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.common.PropertiesOperator;
import com.crawler.entity.KaiJiangInfo;
import com.zhcw.kaijiang.util.StringUtil;

/**
 * 将查詢出來的資訊導出到excel中
 * 
 * @author zhcw
 * 
 */
public class ExportExcal {
	private String czNum = "";
	private String czId = "";
	private String czName = "";
	private String beginIssue = "";
	private String endIssue = "";
	private String pageNum = "";

	public ExportExcal(String beginIssue, String czId, String czName, String czNum, String endIssue, String pageNum) {
		super();
		this.beginIssue = (beginIssue == null ? "" : beginIssue);
		this.endIssue = (endIssue == null ? "" : endIssue);
		this.czId = (czId == null ? "" : czId);
		this.czName = (czName == null ? "" : czName);
		this.czNum = (czNum == null || czNum.trim().equals("") || czNum.trim().equals("null")) ? "30" : czNum;
		this.pageNum = (pageNum == null || pageNum.trim().equals("") || pageNum.trim().equals("null")) ? "1" : pageNum;
	}

	public List<KaiJiangInfo> kaiJiangInfoList() {
		KaijiangInfoService kaijiangInfoService = new KaijiangInfoService();
		if (this.beginIssue!=null && this.beginIssue.trim().length() > 0) {
			return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), 400, this.beginIssue, this.endIssue);
		} else {
			return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), Integer.parseInt(czNum));
		}
	}

	public void export(OutputStream output) throws Exception {
		PropertiesOperator propertiesOperator = new PropertiesOperator();
		String cz_id_css_1 = propertiesOperator.getMessage("cz_id_css_1") == null ? "" : propertiesOperator.getMessage("cz_id_css_1").trim();
		String cz_id_css_2 = propertiesOperator.getMessage("cz_id_css_2") == null ? "" : propertiesOperator.getMessage("cz_id_css_2").trim();
		String cz_id_css_3 = propertiesOperator.getMessage("cz_id_css_3") == null ? "" : propertiesOperator.getMessage("cz_id_css_3").trim();
		String cz_id_css_4 = propertiesOperator.getMessage("cz_id_css_4") == null ? "" : propertiesOperator.getMessage("cz_id_css_4").trim();
		String cz_id_css_5 = propertiesOperator.getMessage("cz_id_css_5") == null ? "" : propertiesOperator.getMessage("cz_id_css_5").trim();
		if (StringUtil.contains(this.czId, cz_id_css_1)) {
			this.exportSSQExcel(output);
		} else if (StringUtil.contains(this.czId, cz_id_css_2)) {
			this.export3DExcel(output);
		}
	}
	private void exportExcel(String type,OutputStream output) throws Exception {
		WritableWorkbook workbook = Workbook.createWorkbook(output);// 建立工作薄
		WritableSheet sheet = workbook.createSheet(this.czName, 0);// 建立第一個工作表,name:工作表名稱
		//設定列寬度
		sheet.setColumnView(0,7);
		sheet.setColumnView(1,15);
		sheet.setColumnView(2,13);
		sheet.setColumnView(3,25);
		sheet.setColumnView(4,15);
		sheet.setColumnView(6,15);
		sheet.setColumnView(8,15);
		sheet.setColumnView(10,15);
		sheet.setColumnView(11,15);
		WritableCellFormat format = new WritableCellFormat();
		format.setAlignment(Alignment.CENTRE);
		format.setBorder(Border.ALL, BorderLineStyle.THIN);
		int row = 0;
		//合并标題行
		sheet.mergeCells(0, row, 11, row);
		
		// 合并
		sheet.mergeCells(0, row + 1, 0, row + 2);
		sheet.mergeCells(1, row + 1, 1, row + 2);
		sheet.mergeCells(2, row + 1, 2, row + 2);
		sheet.mergeCells(3, row + 1, 3, row + 2);
		sheet.mergeCells(4, row + 1, 4, row + 2);
		sheet.mergeCells(11, row + 1, 11, row + 2);

		sheet.mergeCells(5, row + 1, 6, row + 1);
		sheet.mergeCells(7, row + 1, 8, row + 1);
		sheet.mergeCells(9, row + 1, 10, row + 1);

		Label label = null;// 用于寫入文本内容到工作表中去
		// 開始寫入第一行,即标題欄
		if(this.beginIssue.length()>0){
			label = new Label(0, row, czName+"從"+this.beginIssue+" 到 "+this.endIssue+"期的中獎資訊",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
		}else{
			label = new Label(0, row, czName+"的前 "+this.czNum+" 期的中獎資訊",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
		}
		//第二行寫表頭
		label = new Label(0, row + 1, "序号",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(1, row + 1, "開獎日期",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(2, row + 1, "期号",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(3, row + 1, "中獎号碼",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(4, row + 1, "銷售額(元)",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		if(type!=null && type.trim().equals("ssq")){
			label = new Label(5, row + 1, "一等獎",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
			label = new Label(7, row + 1, "二等獎",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
			label = new Label(9, row + 1, "三等獎",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
		}else if(type!=null && type.trim().equals("3d")){
			label = new Label(5, row + 1, "直選",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
			label = new Label(7, row + 1, "組三",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
			label = new Label(9, row + 1, "組六",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
		}
		//隻有雙色球有獎池金額
		if(type!=null && type.trim().equals("ssq")){
			label = new Label(11, row + 1, "獎池(元)",format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
		}
		label = new Label(5, row + 2, "注數",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(6, row + 2, "獎金",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(7, row + 2, "注數",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(8, row + 2, "獎金",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(9, row + 2, "注數",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		label = new Label(10, row + 2, "獎金",format);// 參數依次代表列數、行數、内容
		sheet.addCell(label);// 寫入單元格
		List<KaiJiangInfo> list = this.kaiJiangInfoList();
		int index = 1;
		int num = 2;
		DecimalFormat df = new DecimalFormat("#0.00");
		for (KaiJiangInfo kaiJiangInfo : list) {
			num = num + 1;
			label = new Label(0, num, String.valueOf((Integer.parseInt(pageNum) - 1) * 400 + (index++)),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(1, num, kaiJiangInfo.getKjDate(),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(2, num, kaiJiangInfo.getIssue(),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
			
			if(type!=null && type.trim().equals("ssq")){
				label = new Label(3, num, kaiJiangInfo.getKjZNum() + " " + kaiJiangInfo.getKjTNum(),format);// 參數依次代表列數、行數、内容
				sheet.addCell(label);// 寫入單元格
			}else if(type!=null && type.trim().equals("3d")){
				label = new Label(3, num, kaiJiangInfo.getKjZNum(),format);// 參數依次代表列數、行數、内容
				sheet.addCell(label);// 寫入單元格
			}

			label = new Label(4, num, df.format(kaiJiangInfo.getSales()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(5, num, String.valueOf(kaiJiangInfo.getNoteOne()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(6, num, df.format(kaiJiangInfo.getBonusOne()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(7, num, String.valueOf(kaiJiangInfo.getNoteTwo()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(8, num, df.format(kaiJiangInfo.getBonusTwo()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(9, num, String.valueOf(kaiJiangInfo.getNoteThree()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格

			label = new Label(10, num, df.format(kaiJiangInfo.getBonusThree()),format);// 參數依次代表列數、行數、内容
			sheet.addCell(label);// 寫入單元格
			//隻有雙色球有獎池金額
			if(type!=null && type.trim().equals("ssq")){
				label = new Label(11, num, df.format(kaiJiangInfo.getBonusPool()),format);// 參數依次代表列數、行數、内容
				sheet.addCell(label);// 寫入單元格
			}
		}
		workbook.write();
		workbook.close();
	}
	private void exportSSQExcel(OutputStream output) throws Exception {
		this.exportExcel("ssq", output);
	}

	private void export3DExcel(OutputStream output) throws Exception {
		this.exportExcel("3d", output);
	}
}
           

更多文章見: http://www.16boke.com