jsp页面将数据导出到Excel
一、列表页面的部分代码
- <inputtype="button"class="cxBut2"value="导出数据"onclick="_export()"/>
- function_export(){
- document.location.href="download.jsp?czId=<%=czId%>&czNum=<%=czNum%>&beginIssue=<%=beginIssue%>&endIssue=<%=endIssue%>&pageNum=<%=pageNum%>&czName="+$("#czId").find("option:selected").text();
- }
二、download.jsp
- <%@pagecontentType="application/vnd.ms-excel"language="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%@pageimport="com.zhcw.kaijiang.service.ExportExcal"%>
- <%@pageimport="java.io.OutputStream"%>
- <%
- StringczNum=request.getParameter("czNum");
- StringczId=request.getParameter("czId");
- StringczName=request.getParameter("czName");
- StringbeginIssue=request.getParameter("beginIssue");
- StringendIssue=request.getParameter("endIssue");
- StringpageNum=request.getParameter("pageNum");
- response.resetBuffer();
- response.setHeader("Content-Disposition","attachment;filename="+newString(czName.getBytes("UTF-8"),"iso8859-1")+".xls");//指定下载的文件名
- response.setContentType("application/vnd.ms-excel");
- try{
- ExportExcalexportExcal=newExportExcal(beginIssue,czId,czName,czNum,endIssue,pageNum);
- exportExcal.export(response.getOutputStream());
- }catch(Exceptionex){
- ex.printStackTrace();
- }
- %>
这个页面主要负责接收上一个页面传来的数据,然后调用后台相应的导出方法来实现导出功能。
注意:这两行代码必须要写:
- response.resetBuffer();<prename="code"class="html">response.setContentType("application/vnd.ms-excel");</pre>
- <pre></pre>
- <p></p>
- <pre></pre>
- <p></p>
- <p></p>
- 三、后台处理类<prename="code"class="java">packagecom.zhcw.kaijiang.service;
- importjava.io.File;
- importjava.io.FileOutputStream;
- importjava.io.OutputStream;
- importjava.text.DecimalFormat;
- importjava.util.List;
- importjxl.Workbook;
- importjxl.format.Alignment;
- importjxl.format.Border;
- importjxl.format.BorderLineStyle;
- importjxl.write.Label;
- importjxl.write.WritableCellFormat;
- importjxl.write.WritableSheet;
- importjxl.write.WritableWorkbook;
- importcom.common.PropertiesOperator;
- importcom.crawler.entity.KaiJiangInfo;
- importcom.zhcw.kaijiang.util.StringUtil;
- /**
- *将查询出来的信息导出到excel中
- *
- *@authorzhcw
- *
- */
- publicclassExportExcal{
- privateStringczNum="";
- privateStringczId="";
- privateStringczName="";
- privateStringbeginIssue="";
- privateStringendIssue="";
- privateStringpageNum="";
- publicExportExcal(StringbeginIssue,StringczId,StringczName,StringczNum,StringendIssue,StringpageNum){
- 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;
- }
- publicList<KaiJiangInfo>kaiJiangInfoList(){
- KaijiangInfoServicekaijiangInfoService=newKaijiangInfoService();
- if(this.beginIssue!=null&&this.beginIssue.trim().length()>0){
- returnkaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId),Integer.valueOf(pageNum),400,this.beginIssue,this.endIssue);
- }else{
- returnkaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId),Integer.valueOf(pageNum),Integer.parseInt(czNum));
- }
- }
- publicvoidexport(OutputStreamoutput)throwsException{
- PropertiesOperatorpropertiesOperator=newPropertiesOperator();
- Stringcz_id_css_1=propertiesOperator.getMessage("cz_id_css_1")==null?"":propertiesOperator.getMessage("cz_id_css_1").trim();
- Stringcz_id_css_2=propertiesOperator.getMessage("cz_id_css_2")==null?"":propertiesOperator.getMessage("cz_id_css_2").trim();
- Stringcz_id_css_3=propertiesOperator.getMessage("cz_id_css_3")==null?"":propertiesOperator.getMessage("cz_id_css_3").trim();
- Stringcz_id_css_4=propertiesOperator.getMessage("cz_id_css_4")==null?"":propertiesOperator.getMessage("cz_id_css_4").trim();
- Stringcz_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);
- }elseif(StringUtil.contains(this.czId,cz_id_css_2)){
- this.export3DExcel(output);
- }
- }
- privatevoidexportExcel(Stringtype,OutputStreamoutput)throwsException{
- WritableWorkbookworkbook=Workbook.createWorkbook(output);//创建工作薄
- WritableSheetsheet=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);
- WritableCellFormatformat=newWritableCellFormat();
- format.setAlignment(Alignment.CENTRE);
- format.setBorder(Border.ALL,BorderLineStyle.THIN);
- introw=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);
- Labellabel=null;//用于写入文本内容到工作表中去
- //开始写入第一行,即标题栏
- if(this.beginIssue.length()>0){
- label=newLabel(0,row,czName+"从"+this.beginIssue+"到"+this.endIssue+"期的中奖信息",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }else{
- label=newLabel(0,row,czName+"的前"+this.czNum+"期的中奖信息",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }
- //第二行写表头
- label=newLabel(0,row+1,"序号",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(1,row+1,"开奖日期",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(2,row+1,"期号",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(3,row+1,"中奖号码",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(4,row+1,"销售额(元)",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- if(type!=null&&type.trim().equals("ssq")){
- label=newLabel(5,row+1,"一等奖",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(7,row+1,"二等奖",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(9,row+1,"三等奖",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }elseif(type!=null&&type.trim().equals("3d")){
- label=newLabel(5,row+1,"直选",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(7,row+1,"组三",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(9,row+1,"组六",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }
- //只有双色球有奖池金额
- if(type!=null&&type.trim().equals("ssq")){
- label=newLabel(11,row+1,"奖池(元)",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }
- label=newLabel(5,row+2,"注数",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(6,row+2,"奖金",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(7,row+2,"注数",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(8,row+2,"奖金",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(9,row+2,"注数",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(10,row+2,"奖金",format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- List<KaiJiangInfo>list=this.kaiJiangInfoList();
- intindex=1;
- intnum=2;
- DecimalFormatdf=newDecimalFormat("#0.00");
- for(KaiJiangInfokaiJiangInfo:list){
- num=num+1;
- label=newLabel(0,num,String.valueOf((Integer.parseInt(pageNum)-1)*400+(index++)),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(1,num,kaiJiangInfo.getKjDate(),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(2,num,kaiJiangInfo.getIssue(),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- if(type!=null&&type.trim().equals("ssq")){
- label=newLabel(3,num,kaiJiangInfo.getKjZNum()+""+kaiJiangInfo.getKjTNum(),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }elseif(type!=null&&type.trim().equals("3d")){
- label=newLabel(3,num,kaiJiangInfo.getKjZNum(),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }
- label=newLabel(4,num,df.format(kaiJiangInfo.getSales()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(5,num,String.valueOf(kaiJiangInfo.getNoteOne()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(6,num,df.format(kaiJiangInfo.getBonusOne()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(7,num,String.valueOf(kaiJiangInfo.getNoteTwo()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(8,num,df.format(kaiJiangInfo.getBonusTwo()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(9,num,String.valueOf(kaiJiangInfo.getNoteThree()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- label=newLabel(10,num,df.format(kaiJiangInfo.getBonusThree()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- //只有双色球有奖池金额
- if(type!=null&&type.trim().equals("ssq")){
- label=newLabel(11,num,df.format(kaiJiangInfo.getBonusPool()),format);//参数依次代表列数、行数、内容
- sheet.addCell(label);//写入单元格
- }
- }
- workbook.write();
- workbook.close();
- }
- privatevoidexportSSQExcel(OutputStreamoutput)throwsException{
- this.exportExcel("ssq",output);
- }
- privatevoidexport3DExcel(OutputStreamoutput)throwsException{
- this.exportExcel("3d",output);
- }
- }
- </pre><br>
- <br>
jsp页面将数据导出到Excel相关推荐
- ts获取服务器数据_怎么使用Satruts2实现页面列表数据导出到Excel表格
Java codeprivate InputStream excelFile; public void setExcelFile(InputStream excelFile) { this.excel ...
- 记录~页面table数据导出到excel时,数字或字符串显示成科学计数法的解决办法
在表格中的<td>标签中添加样式:style="mso-number-format:'\@';" <td style="width:90px;mso-n ...
- 查询php 输出表格,php输出excel表格数据-PHP如何将查询出来的数据导出成excel表格(最好做......
PHP如何将查询出来的数据导出成excel表格(最好做... php 把数据导出excel表格有多种方法,使用 phpExcel 等,以下代码接通过 header 生成 excel 文件的代码示例: ...
- java导出excel 序号_java web将数据导出为Excel格式文件代码片段
本文实例为大家分享了java web将数据导出为Excel格式文件的具体代码,供大家参考,具体内容如下 1.jsp代码 2.js代码 function getVerExcel() { window.l ...
- 关于数据导出成excel表
关于数据导出成excel表 咱们这里分享简单导出成excel表和筛选导出excel表,希望对各位有帮助,欢迎大家交流和点赞!!!! 我在这里使用的是一个导出工具类,如下: package cn.ms. ...
- 使用java在后台将数据导出为excel文件
本文主要讲的是怎么使用java将数据导出为Excel文件,xls格式的. 例如:我从前台查询到的数据,要把数据进行导出为excl格式的文件.需要将前台的查询条件(数据)传递到后台,后台拿到数据,写sq ...
- 表格导出计算机,电脑怎么导出excel表格数据-如何将百度指数数据导出到Excel表格...
系统没有导出功能,电脑表格里的数据有什么方法拿... 将网页表格数据导入到Excel中的方法: 第一步,将包括所需表格的网页打开,并按CTRL C把网址复制到剪贴板,以备下一步使用. 第二步,打开运行 ...
- 数据库数据导出到Excel
数据库数据导出到Excel 从数据库中获取数据,使用java建立一个新建Excel,添加表头,并将数据存储到Excel表格中的对应位置上. 在新建一个Excel时,我们会需要定义一些特别的Excel格 ...
- matlab将求解值导出数据,MATLAB之将mat中的数据导出到Excel文件
MATLAB之将mat中的数据导出到Excel文件 原始文档: https://www.yuque.com/lart/tools/cnedve 文章目录 MATLAB之将mat中的数据导出到Excel ...
最新文章
- Python初学问题-if/else的运用
- Python程序开发——第三章 列表与元组
- [2020.11.4NOIP模拟赛]简单的打击【NTT】
- JOOQ事实:从JPA批注到JOOQ表映射
- 后端数据库的初步设计
- zuc算法代码详解_最短路算法-dijkstra代码与案例详解
- c++中delete和析构函数之间的区别
- 【C语言】23-typedef
- 腾讯x5内核(TBS)简单集成封装
- 类和对象12:容器方法
- 个人博客网站搭建-WordPress-NameSilo-云左虚拟主机
- 图片怎么识别文字?超实用的文字识别技巧分享,分享给你
- 关于快递查询接口的实现
- 使用python Mayavi 绘制矢量场图
- 美国波多里奇国家质量奖(MBNQA)简介
- apple configurator 2 提取ipa文件
- 你没看错!TCL品牌日10万台洗衣机免费送
- 收藏一个网站可以在线生成带图片的二维码
- 嵌入式混合操作系统TOS源码开源
- Baidu、Google、Soso等搜索引擎网站登录入口
热门文章
- 【资料整理】scribe安装配置
- Thrift协议的服务模型
- 为什么要用 SpringMVC 的 SessionStatus
- iptables的连接追踪机制和nf_conntrack调优
- 02-C#入门(枚举、结构等)
- find a ubuntu featue
- 分享2个第三方社会化分享按钮及分享工具
- AgreementMaker:Efficient Matching for Large Real-World 翻译
- [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
- Redis+Keepalived内存数据库集群配置