一、列表页面的部分代码

[html]view plaincopy
  1. <inputtype="button"class="cxBut2"value="导出数据"onclick="_export()"/>
[javascript]view plaincopy
  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 plaincopy
  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 plaincopy
  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>

jsp页面将数据导出到Excel相关推荐

  1. ts获取服务器数据_怎么使用Satruts2实现页面列表数据导出到Excel表格

    Java codeprivate InputStream excelFile; public void setExcelFile(InputStream excelFile) { this.excel ...

  2. 记录~页面table数据导出到excel时,数字或字符串显示成科学计数法的解决办法

    在表格中的<td>标签中添加样式:style="mso-number-format:'\@';" <td style="width:90px;mso-n ...

  3. 查询php 输出表格,php输出excel表格数据-PHP如何将查询出来的数据导出成excel表格(最好做......

    PHP如何将查询出来的数据导出成excel表格(最好做... php 把数据导出excel表格有多种方法,使用 phpExcel 等,以下代码接通过 header 生成 excel 文件的代码示例: ...

  4. java导出excel 序号_java web将数据导出为Excel格式文件代码片段

    本文实例为大家分享了java web将数据导出为Excel格式文件的具体代码,供大家参考,具体内容如下 1.jsp代码 2.js代码 function getVerExcel() { window.l ...

  5. 关于数据导出成excel表

    关于数据导出成excel表 咱们这里分享简单导出成excel表和筛选导出excel表,希望对各位有帮助,欢迎大家交流和点赞!!!! 我在这里使用的是一个导出工具类,如下: package cn.ms. ...

  6. 使用java在后台将数据导出为excel文件

    本文主要讲的是怎么使用java将数据导出为Excel文件,xls格式的. 例如:我从前台查询到的数据,要把数据进行导出为excl格式的文件.需要将前台的查询条件(数据)传递到后台,后台拿到数据,写sq ...

  7. 表格导出计算机,电脑怎么导出excel表格数据-如何将百度指数数据导出到Excel表格...

    系统没有导出功能,电脑表格里的数据有什么方法拿... 将网页表格数据导入到Excel中的方法: 第一步,将包括所需表格的网页打开,并按CTRL C把网址复制到剪贴板,以备下一步使用. 第二步,打开运行 ...

  8. 数据库数据导出到Excel

    数据库数据导出到Excel 从数据库中获取数据,使用java建立一个新建Excel,添加表头,并将数据存储到Excel表格中的对应位置上. 在新建一个Excel时,我们会需要定义一些特别的Excel格 ...

  9. matlab将求解值导出数据,MATLAB之将mat中的数据导出到Excel文件

    MATLAB之将mat中的数据导出到Excel文件 原始文档: https://www.yuque.com/lart/tools/cnedve 文章目录 MATLAB之将mat中的数据导出到Excel ...

最新文章

  1. Python初学问题-if/else的运用
  2. Python程序开发——第三章 列表与元组
  3. [2020.11.4NOIP模拟赛]简单的打击【NTT】
  4. JOOQ事实:从JPA批注到JOOQ表映射
  5. 后端数据库的初步设计
  6. zuc算法代码详解_最短路算法-dijkstra代码与案例详解
  7. c++中delete和析构函数之间的区别
  8. 【C语言】23-typedef
  9. 腾讯x5内核(TBS)简单集成封装
  10. 类和对象12:容器方法
  11. 个人博客网站搭建-WordPress-NameSilo-云左虚拟主机
  12. 图片怎么识别文字?超实用的文字识别技巧分享,分享给你
  13. 关于快递查询接口的实现
  14. 使用python Mayavi 绘制矢量场图
  15. 美国波多里奇国家质量奖(MBNQA)简介
  16. apple configurator 2 提取ipa文件
  17. 你没看错!TCL品牌日10万台洗衣机免费送
  18. 收藏一个网站可以在线生成带图片的二维码
  19. 嵌入式混合操作系统TOS源码开源
  20. Baidu、Google、Soso等搜索引擎网站登录入口

热门文章

  1. 【资料整理】scribe安装配置
  2. Thrift协议的服务模型
  3. 为什么要用 SpringMVC 的 SessionStatus
  4. iptables的连接追踪机制和nf_conntrack调优
  5. 02-C#入门(枚举、结构等)
  6. find a ubuntu featue
  7. 分享2个第三方社会化分享按钮及分享工具
  8. AgreementMaker:Efficient Matching for Large Real-World 翻译
  9. [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
  10. Redis+Keepalived内存数据库集群配置