jsp页面将数据导出到Excel

一、列表页面的部分代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值