jsp页面将数据导出到Excel

一、列表页面的部分代码

[html]  view plain copy
  1. <input type="button" class="cxBut2" value="导出数据" onclick="_export()"/>   
[javascript]  view plain copy
  1. function _export(){  
  2.     document.location.href="download.jsp?czId=<%=czId%>&czNum=<%=czNum%>&beginIssue=<%=beginIssue%>&endIssue=<%=endIssue%>&pageNum=<%=pageNum%>&czName="+$("#czId").find("option:selected").text();  
  3. }  

二、download.jsp

[html]  view plain copy
  1. <%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@page import="com.zhcw.kaijiang.service.ExportExcal"%>  
  3. <%@page import="java.io.OutputStream"%>  
  4. <%  
  5.     String czNum = request.getParameter("czNum");  
  6.     String czId = request.getParameter("czId");  
  7.     String czName = request.getParameter("czName");  
  8.     String beginIssue = request.getParameter("beginIssue");  
  9.     String endIssue = request.getParameter("endIssue");  
  10.     String pageNum = request.getParameter("pageNum");  
  11.     response.resetBuffer();  
  12.     response.setHeader("Content-Disposition", "attachment;filename="+ new String(czName.getBytes("UTF-8"), "iso8859-1")+".xls");//指定下载的文件名  
  13.     response.setContentType("application/vnd.ms-excel");  
  14.     try{  
  15.         ExportExcal exportExcal = new ExportExcal(beginIssue, czId, czName, czNum, endIssue, pageNum);  
  16.         exportExcal.export(response.getOutputStream());  
  17.     }catch(Exception ex){  
  18.         ex.printStackTrace();  
  19.     }  
  20. %>  
这个页面主要负责接收上一个页面传来的数据,然后调用后台相应的导出方法来实现导出功能。

注意:这两行代码必须要写:

[html]  view plain copy
  1. response.resetBuffer();<pre name="code" class="html">response.setContentType("application/vnd.ms-excel");</pre>  
  2. <pre></pre>  
  3. <p></p>  
  4. <pre></pre>  
  5. <p></p>  
  6. <p></p>  
  7. 三、后台处理类<pre name="code" class="java">package com.zhcw.kaijiang.service;  
  8.   
  9. import java.io.File;  
  10. import java.io.FileOutputStream;  
  11. import java.io.OutputStream;  
  12. import java.text.DecimalFormat;  
  13. import java.util.List;  
  14.   
  15. import jxl.Workbook;  
  16. import jxl.format.Alignment;  
  17. import jxl.format.Border;  
  18. import jxl.format.BorderLineStyle;  
  19. import jxl.write.Label;  
  20. import jxl.write.WritableCellFormat;  
  21. import jxl.write.WritableSheet;  
  22. import jxl.write.WritableWorkbook;  
  23.   
  24. import com.common.PropertiesOperator;  
  25. import com.crawler.entity.KaiJiangInfo;  
  26. import com.zhcw.kaijiang.util.StringUtil;  
  27.   
  28. /**  
  29.  * 将查询出来的信息导出到excel中  
  30.  *   
  31.  * @author zhcw  
  32.  *   
  33.  */  
  34. public class ExportExcal {  
  35.     private String czNum = "";  
  36.     private String czId = "";  
  37.     private String czName = "";  
  38.     private String beginIssue = "";  
  39.     private String endIssue = "";  
  40.     private String pageNum = "";  
  41.   
  42.     public ExportExcal(String beginIssue, String czId, String czName, String czNum, String endIssue, String pageNum) {  
  43.         super();  
  44.         this.beginIssue = (beginIssue == null ? "" : beginIssue);  
  45.         this.endIssue = (endIssue == null ? "" : endIssue);  
  46.         this.czId = (czId == null ? "" : czId);  
  47.         this.czName = (czName == null ? "" : czName);  
  48.         this.czNum = (czNum == null || czNum.trim().equals("") || czNum.trim().equals("null")) ? "30" : czNum;  
  49.         this.pageNum = (pageNum == null || pageNum.trim().equals("") || pageNum.trim().equals("null")) ? "1" : pageNum;  
  50.     }  
  51.   
  52.     public List<KaiJiangInfo> kaiJiangInfoList() {  
  53.         KaijiangInfoService kaijiangInfoService = new KaijiangInfoService();  
  54.         if (this.beginIssue!=null && this.beginIssue.trim().length() > 0) {  
  55.             return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), 400, this.beginIssue, this.endIssue);  
  56.         } else {  
  57.             return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), Integer.parseInt(czNum));  
  58.         }  
  59.     }  
  60.   
  61.     public void export(OutputStream output) throws Exception {  
  62.         PropertiesOperator propertiesOperator = new PropertiesOperator();  
  63.         String cz_id_css_1 = propertiesOperator.getMessage("cz_id_css_1") == null ? "" : propertiesOperator.getMessage("cz_id_css_1").trim();  
  64.         String cz_id_css_2 = propertiesOperator.getMessage("cz_id_css_2") == null ? "" : propertiesOperator.getMessage("cz_id_css_2").trim();  
  65.         String cz_id_css_3 = propertiesOperator.getMessage("cz_id_css_3") == null ? "" : propertiesOperator.getMessage("cz_id_css_3").trim();  
  66.         String cz_id_css_4 = propertiesOperator.getMessage("cz_id_css_4") == null ? "" : propertiesOperator.getMessage("cz_id_css_4").trim();  
  67.         String cz_id_css_5 = propertiesOperator.getMessage("cz_id_css_5") == null ? "" : propertiesOperator.getMessage("cz_id_css_5").trim();  
  68.         if (StringUtil.contains(this.czId, cz_id_css_1)) {  
  69.             this.exportSSQExcel(output);  
  70.         } else if (StringUtil.contains(this.czId, cz_id_css_2)) {  
  71.             this.export3DExcel(output);  
  72.         }  
  73.     }  
  74.     private void exportExcel(String type,OutputStream output) throws Exception {  
  75.         WritableWorkbook workbook = Workbook.createWorkbook(output);// 创建工作薄  
  76.         WritableSheet sheet = workbook.createSheet(this.czName, 0);// 创建第一个工作表,name:工作表名称  
  77.         //设置列宽度  
  78.         sheet.setColumnView(0,7);  
  79.         sheet.setColumnView(1,15);  
  80.         sheet.setColumnView(2,13);  
  81.         sheet.setColumnView(3,25);  
  82.         sheet.setColumnView(4,15);  
  83.         sheet.setColumnView(6,15);  
  84.         sheet.setColumnView(8,15);  
  85.         sheet.setColumnView(10,15);  
  86.         sheet.setColumnView(11,15);  
  87.         WritableCellFormat format = new WritableCellFormat();  
  88.         format.setAlignment(Alignment.CENTRE);  
  89.         format.setBorder(Border.ALL, BorderLineStyle.THIN);  
  90.         int row = 0;  
  91.         //合并标题行  
  92.         sheet.mergeCells(0, row, 11, row);  
  93.           
  94.         // 合并  
  95.         sheet.mergeCells(0, row + 1, 0, row + 2);  
  96.         sheet.mergeCells(1, row + 1, 1, row + 2);  
  97.         sheet.mergeCells(2, row + 1, 2, row + 2);  
  98.         sheet.mergeCells(3, row + 1, 3, row + 2);  
  99.         sheet.mergeCells(4, row + 1, 4, row + 2);  
  100.         sheet.mergeCells(11, row + 1, 11, row + 2);  
  101.   
  102.         sheet.mergeCells(5, row + 1, 6, row + 1);  
  103.         sheet.mergeCells(7, row + 1, 8, row + 1);  
  104.         sheet.mergeCells(9, row + 1, 10, row + 1);  
  105.   
  106.         Label label = null;// 用于写入文本内容到工作表中去  
  107.         // 开始写入第一行,即标题栏  
  108.         if(this.beginIssue.length()>0){  
  109.             label = new Label(0, row, czName+"从"+this.beginIssue+" 到 "+this.endIssue+"期的中奖信息",format);// 参数依次代表列数、行数、内容  
  110.             sheet.addCell(label);// 写入单元格  
  111.         }else{  
  112.             label = new Label(0, row, czName+"的前 "+this.czNum+" 期的中奖信息",format);// 参数依次代表列数、行数、内容  
  113.             sheet.addCell(label);// 写入单元格  
  114.         }  
  115.         //第二行写表头  
  116.         label = new Label(0, row + 1, "序号",format);// 参数依次代表列数、行数、内容  
  117.         sheet.addCell(label);// 写入单元格  
  118.         label = new Label(1, row + 1, "开奖日期",format);// 参数依次代表列数、行数、内容  
  119.         sheet.addCell(label);// 写入单元格  
  120.         label = new Label(2, row + 1, "期号",format);// 参数依次代表列数、行数、内容  
  121.         sheet.addCell(label);// 写入单元格  
  122.         label = new Label(3, row + 1, "中奖号码",format);// 参数依次代表列数、行数、内容  
  123.         sheet.addCell(label);// 写入单元格  
  124.         label = new Label(4, row + 1, "销售额(元)",format);// 参数依次代表列数、行数、内容  
  125.         sheet.addCell(label);// 写入单元格  
  126.         if(type!=null && type.trim().equals("ssq")){  
  127.             label = new Label(5, row + 1, "一等奖",format);// 参数依次代表列数、行数、内容  
  128.             sheet.addCell(label);// 写入单元格  
  129.             label = new Label(7, row + 1, "二等奖",format);// 参数依次代表列数、行数、内容  
  130.             sheet.addCell(label);// 写入单元格  
  131.             label = new Label(9, row + 1, "三等奖",format);// 参数依次代表列数、行数、内容  
  132.             sheet.addCell(label);// 写入单元格  
  133.         }else if(type!=null && type.trim().equals("3d")){  
  134.             label = new Label(5, row + 1, "直选",format);// 参数依次代表列数、行数、内容  
  135.             sheet.addCell(label);// 写入单元格  
  136.             label = new Label(7, row + 1, "组三",format);// 参数依次代表列数、行数、内容  
  137.             sheet.addCell(label);// 写入单元格  
  138.             label = new Label(9, row + 1, "组六",format);// 参数依次代表列数、行数、内容  
  139.             sheet.addCell(label);// 写入单元格  
  140.         }  
  141.         //只有双色球有奖池金额  
  142.         if(type!=null && type.trim().equals("ssq")){  
  143.             label = new Label(11, row + 1, "奖池(元)",format);// 参数依次代表列数、行数、内容  
  144.             sheet.addCell(label);// 写入单元格  
  145.         }  
  146.         label = new Label(5, row + 2, "注数",format);// 参数依次代表列数、行数、内容  
  147.         sheet.addCell(label);// 写入单元格  
  148.         label = new Label(6, row + 2, "奖金",format);// 参数依次代表列数、行数、内容  
  149.         sheet.addCell(label);// 写入单元格  
  150.         label = new Label(7, row + 2, "注数",format);// 参数依次代表列数、行数、内容  
  151.         sheet.addCell(label);// 写入单元格  
  152.         label = new Label(8, row + 2, "奖金",format);// 参数依次代表列数、行数、内容  
  153.         sheet.addCell(label);// 写入单元格  
  154.         label = new Label(9, row + 2, "注数",format);// 参数依次代表列数、行数、内容  
  155.         sheet.addCell(label);// 写入单元格  
  156.         label = new Label(10, row + 2, "奖金",format);// 参数依次代表列数、行数、内容  
  157.         sheet.addCell(label);// 写入单元格  
  158.         List<KaiJiangInfo> list = this.kaiJiangInfoList();  
  159.         int index = 1;  
  160.         int num = 2;  
  161.         DecimalFormat df = new DecimalFormat("#0.00");  
  162.         for (KaiJiangInfo kaiJiangInfo : list) {  
  163.             num = num + 1;  
  164.             label = new Label(0, num, String.valueOf((Integer.parseInt(pageNum) - 1) * 400 + (index++)),format);// 参数依次代表列数、行数、内容  
  165.             sheet.addCell(label);// 写入单元格  
  166.   
  167.             label = new Label(1, num, kaiJiangInfo.getKjDate(),format);// 参数依次代表列数、行数、内容  
  168.             sheet.addCell(label);// 写入单元格  
  169.   
  170.             label = new Label(2, num, kaiJiangInfo.getIssue(),format);// 参数依次代表列数、行数、内容  
  171.             sheet.addCell(label);// 写入单元格  
  172.               
  173.             if(type!=null && type.trim().equals("ssq")){  
  174.                 label = new Label(3, num, kaiJiangInfo.getKjZNum() + " " + kaiJiangInfo.getKjTNum(),format);// 参数依次代表列数、行数、内容  
  175.                 sheet.addCell(label);// 写入单元格  
  176.             }else if(type!=null && type.trim().equals("3d")){  
  177.                 label = new Label(3, num, kaiJiangInfo.getKjZNum(),format);// 参数依次代表列数、行数、内容  
  178.                 sheet.addCell(label);// 写入单元格  
  179.             }  
  180.   
  181.             label = new Label(4, num, df.format(kaiJiangInfo.getSales()),format);// 参数依次代表列数、行数、内容  
  182.             sheet.addCell(label);// 写入单元格  
  183.   
  184.             label = new Label(5, num, String.valueOf(kaiJiangInfo.getNoteOne()),format);// 参数依次代表列数、行数、内容  
  185.             sheet.addCell(label);// 写入单元格  
  186.   
  187.             label = new Label(6, num, df.format(kaiJiangInfo.getBonusOne()),format);// 参数依次代表列数、行数、内容  
  188.             sheet.addCell(label);// 写入单元格  
  189.   
  190.             label = new Label(7, num, String.valueOf(kaiJiangInfo.getNoteTwo()),format);// 参数依次代表列数、行数、内容  
  191.             sheet.addCell(label);// 写入单元格  
  192.   
  193.             label = new Label(8, num, df.format(kaiJiangInfo.getBonusTwo()),format);// 参数依次代表列数、行数、内容  
  194.             sheet.addCell(label);// 写入单元格  
  195.   
  196.             label = new Label(9, num, String.valueOf(kaiJiangInfo.getNoteThree()),format);// 参数依次代表列数、行数、内容  
  197.             sheet.addCell(label);// 写入单元格  
  198.   
  199.             label = new Label(10, num, df.format(kaiJiangInfo.getBonusThree()),format);// 参数依次代表列数、行数、内容  
  200.             sheet.addCell(label);// 写入单元格  
  201.             //只有双色球有奖池金额  
  202.             if(type!=null && type.trim().equals("ssq")){  
  203.                 label = new Label(11, num, df.format(kaiJiangInfo.getBonusPool()),format);// 参数依次代表列数、行数、内容  
  204.                 sheet.addCell(label);// 写入单元格  
  205.             }  
  206.         }  
  207.         workbook.write();  
  208.         workbook.close();  
  209.     }  
  210.     private void exportSSQExcel(OutputStream output) throws Exception {  
  211.         this.exportExcel("ssq", output);  
  212.     }  
  213.   
  214.     private void export3DExcel(OutputStream output) throws Exception {  
  215.         this.exportExcel("3d", output);  
  216.     }  
  217. }  
  218. </pre><br>  
  219. <br>  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值