jsp导出excel

     jsp导出excel有很多种方法,在此介绍本人认为简单的一种,
前提:能在jsp页面取到要导出的内容,即 request能得到导出的数据,然后代码如下


ExpandedBlockStart.gif ContractedBlock.gif dot.gif@ page language="java" contentType="text/html;charset=gb2312"%>
ExpandedBlockStart.gifContractedBlock.gifdot.gif@ page language="java"  import="java.util.*,
InBlock.gif                                  org.apache.poi.hssf.usermodel.HSSFWorkbook,
InBlock.gif                                  org.apache.poi.hssf.usermodel.HSSFSheet,
InBlock.gif                                  org.apache.poi.hssf.usermodel.HSSFRow,
InBlock.gif                                  org.apache.poi.hssf.usermodel.HSSFCell,
ExpandedBlockEnd.gif                                  java.text.DecimalFormat"
%>
ExpandedBlockStart.gifContractedBlock.gifdot.gif
InBlock.gif    response.reset();
InBlock.gif    response.setContentType("application/msexcel");
InBlock.gif    response.setHeader("Content-disposition","inline;filename=untitled.xls");//定义文件名
InBlock.gif    DecimalFormat f = new DecimalFormat("#,##0.00");
InBlock.gif    HSSFWorkbook wb = new HSSFWorkbook();
InBlock.gif    HSSFSheet sheet = wb.createSheet("sheet1");
InBlock.gif    String[] taxpayerid = request.getParameterValues("taxpayerid");
InBlock.gif    String[] taxpayername = request.getParameterValues("taxpayername");
InBlock.gif    String[] tax = request.getParameterValues("tax");
InBlock.gif    String[] taxreduce = request.getParameterValues("taxreduce");
InBlock.gif    String[] deratereasonname = request.getParameterValues("deratereasonname");
InBlock.gif    String[] orgdeptname = request.getParameterValues("orgdeptname");
InBlock.gif    String[] operatortime = request.getParameterValues("operatortime");
InBlock.gif    String[] declaredate = request.getParameterValues("declaredate");
InBlock.gif    String[] taxtermbegin = request.getParameterValues("taxtermbegin");
InBlock.gif    String[] taxtermend = request.getParameterValues("taxtermend");
InBlock.gif
InBlock.gif//以下以写表头
InBlock.gif        //表头为第一行
InBlock.gif      HSSFRow row = sheet.createRow((short) 0);
InBlock.gif//定义10列
InBlock.gif         HSSFCell cell1 = row.createCell((short) 0);
InBlock.gif        HSSFCell cell2 = row.createCell((short) 1);
InBlock.gif        HSSFCell cell3 = row.createCell((short) 2);
InBlock.gif        HSSFCell cell4 = row.createCell((short) 3);
InBlock.gif        HSSFCell cell5 = row.createCell((short) 4);
InBlock.gif        HSSFCell cell6 = row.createCell((short) 5);
InBlock.gif        HSSFCell cell7 = row.createCell((short) 6);
InBlock.gif        HSSFCell cell8 = row.createCell((short) 7);
InBlock.gif        HSSFCell cell9 = row.createCell((short) 8);
InBlock.gif        HSSFCell cell10 = row.createCell((short) 9);
InBlock.gif
InBlock.gif        cell1.setEncoding((short) 1);
InBlock.gif        cell1.setCellType(1);
InBlock.gif        cell2.setEncoding((short) 1);
InBlock.gif        cell2.setCellType(1);
InBlock.gif        cell3.setEncoding((short) 1);
InBlock.gif        cell3.setCellType(1);
InBlock.gif        cell4.setEncoding((short) 1);
InBlock.gif        cell4.setCellType(1);
InBlock.gif        cell5.setEncoding((short) 1);
InBlock.gif        cell5.setCellType(0);
InBlock.gif        cell6.setEncoding((short) 1);
InBlock.gif        cell6.setCellType(1);
InBlock.gif        cell7.setEncoding((short) 1);
InBlock.gif        cell7.setCellType(1);
InBlock.gif        cell8.setEncoding((short) 1);
InBlock.gif        cell8.setCellType(1);
InBlock.gif        cell9.setEncoding((short) 1);
InBlock.gif        cell9.setCellType(1);
InBlock.gif        cell10.setEncoding((short) 1);
InBlock.gif        cell10.setCellType(1);
InBlock.gif//定义表头的内容
InBlock.gif        cell1.setCellValue("纳税人管理码");
InBlock.gif        cell2.setCellValue("纳税人名称");
InBlock.gif        cell3.setCellValue("税种");
InBlock.gif        cell4.setCellValue("减免金额");
InBlock.gif        cell5.setCellValue("减免原因");
InBlock.gif        cell6.setCellValue("征收单位");
InBlock.gif        cell7.setCellValue("操作日期");
InBlock.gif        cell8.setCellValue("申报日期");
InBlock.gif        cell9.setCellValue("所属期起");
InBlock.gif        cell10.setCellValue("所属期止");
InBlock.gif
InBlock.gif
InBlock.gif    for(int i= 0; i < taxpayerid.length; i++){
InBlock.gif//定义数据从第二行开始       
InBlock.gif  row = sheet.createRow((short) i+1);
InBlock.gif                cell1 = row.createCell((short) 0);
InBlock.gif                cell2 = row.createCell((short) 1);
InBlock.gif                cell3 = row.createCell((short) 2);
InBlock.gif                cell4 = row.createCell((short) 3);
InBlock.gif                cell5 = row.createCell((short) 4);
InBlock.gif                cell6 = row.createCell((short) 5);
InBlock.gif                cell7 = row.createCell((short) 6);
InBlock.gif                cell8 = row.createCell((short) 7);
InBlock.gif                cell9 = row.createCell((short) 8);
InBlock.gif                cell10 = row.createCell((short) 9);
InBlock.gif
InBlock.gif               cell1.setEncoding((short) 1);
InBlock.gif               cell1.setCellType(1);
InBlock.gif               cell2.setEncoding((short) 1);
InBlock.gif               cell2.setCellType(1);
InBlock.gif               cell3.setEncoding((short) 1);
InBlock.gif               cell3.setCellType(1);
InBlock.gif               cell4.setEncoding((short) 1);
InBlock.gif               cell4.setCellType(1);
InBlock.gif               cell5.setEncoding((short) 1);
InBlock.gif               cell5.setCellType(0);
InBlock.gif               cell6.setEncoding((short) 1);
InBlock.gif               cell6.setCellType(1);
InBlock.gif               cell7.setEncoding((short) 1);
InBlock.gif               cell7.setCellType(1);
InBlock.gif               cell8.setEncoding((short) 1);
InBlock.gif               cell8.setCellType(1);
InBlock.gif               cell9.setEncoding((short) 1);
InBlock.gif               cell9.setCellType(1);
InBlock.gif               cell10.setEncoding((short) 1);
InBlock.gif               cell10.setCellType(1);
InBlock.gif
InBlock.gif//填充内容
InBlock.gif
InBlock.gif        cell1.setCellValue(taxpayerid[i]);
InBlock.gif        cell2.setCellValue(taxpayername[i]);
InBlock.gif        cell3.setCellValue(tax[i]);
InBlock.gif        cell4.setCellValue(f.parse(taxreduce[i].trim()).doubleValue());
InBlock.gif        cell5.setCellValue(deratereasonname[i]);
InBlock.gif        cell6.setCellValue(orgdeptname[i]);
InBlock.gif        cell7.setCellValue(operatortime[i].substring(0,16));
InBlock.gif        cell8.setCellValue(declaredate[i].substring(0,16));
InBlock.gif        cell9.setCellValue(taxtermbegin[i].substring(0,16));
InBlock.gif        cell10.setCellValue(taxtermend[i].substring(0,16));
InBlock.gif    }
InBlock.gif    wb.write(response.getOutputStream());
InBlock.gif    response.getOutputStream().flush();
ExpandedBlockEnd.gif    response.getOutputStream().close();
None.gif
%>


代码比较简单,首先把取得到的数据定义为一系列数组,然后定义表头,然后把取得的数据做为excel数据对应的放入,对poi有何疑问请参考 http://java2.5341.com/3.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/134308/viewspace-140577/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/134308/viewspace-140577/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值