piliskys

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  25 随笔 :: 0 文章 :: 40 评论 :: 0 Trackbacks
     jsp导出excel有很多种方法,在此介绍本人认为简单的一种,
前提:能在jsp页面取到要导出的内容,即 request能得到导出的数据,然后代码如下
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page language="java"  import="java.util.*,
                                  org.apache.poi.hssf.usermodel.HSSFWorkbook,
                                  org.apache.poi.hssf.usermodel.HSSFSheet,
                                  org.apache.poi.hssf.usermodel.HSSFRow,
                                  org.apache.poi.hssf.usermodel.HSSFCell,
                                  java.text.DecimalFormat
"
%>
<%
    response.reset();
    response.setContentType(
"application/msexcel");
    response.setHeader(
"Content-disposition","inline;filename=untitled.xls");//定义文件名
    DecimalFormat f 
= new DecimalFormat("#,##0.00");
    HSSFWorkbook wb 
= new HSSFWorkbook();
    HSSFSheet sheet 
= wb.createSheet("sheet1");
    
String[] taxpayerid = request.getParameterValues("taxpayerid");
    
String[] taxpayername = request.getParameterValues("taxpayername");
    
String[] tax = request.getParameterValues("tax");
    
String[] taxreduce = request.getParameterValues("taxreduce");
    
String[] deratereasonname = request.getParameterValues("deratereasonname");
    
String[] orgdeptname = request.getParameterValues("orgdeptname");
    
String[] operatortime = request.getParameterValues("operatortime");
    
String[] declaredate = request.getParameterValues("declaredate");
    
String[] taxtermbegin = request.getParameterValues("taxtermbegin");
    
String[] taxtermend = request.getParameterValues("taxtermend");

//以下以写表头
        
//表头为第一行
      HSSFRow row 
= sheet.createRow((short) 0);
//定义10列
         HSSFCell cell1 
= row.createCell((short) 0);
        HSSFCell cell2 
= row.createCell((short) 1);
        HSSFCell cell3 
= row.createCell((short) 2);
        HSSFCell cell4 
= row.createCell((short) 3);
        HSSFCell cell5 
= row.createCell((short) 4);
        HSSFCell cell6 
= row.createCell((short) 5);
        HSSFCell cell7 
= row.createCell((short) 6);
        HSSFCell cell8 
= row.createCell((short) 7);
        HSSFCell cell9 
= row.createCell((short) 8);
        HSSFCell cell10 
= row.createCell((short) 9);

        cell1.setEncoding((short) 
1);
        cell1.setCellType(
1);
        cell2.setEncoding((short) 
1);
        cell2.setCellType(
1);
        cell3.setEncoding((short) 
1);
        cell3.setCellType(
1);
        cell4.setEncoding((short) 
1);
        cell4.setCellType(
1);
        cell5.setEncoding((short) 
1);
        cell5.setCellType(
0);
        cell6.setEncoding((short) 
1);
        cell6.setCellType(
1);
        cell7.setEncoding((short) 
1);
        cell7.setCellType(
1);
        cell8.setEncoding((short) 
1);
        cell8.setCellType(
1);
        cell9.setEncoding((short) 
1);
        cell9.setCellType(
1);
        cell10.setEncoding((short) 
1);
        cell10.setCellType(
1);
//定义表头的内容
        cell1.setCellValue(
"纳税人管理码");
        cell2.setCellValue(
"纳税人名称");
        cell3.setCellValue(
"税种");
        cell4.setCellValue(
"减免金额");
        cell5.setCellValue(
"减免原因");
        cell6.setCellValue(
"征收单位");
        cell7.setCellValue(
"操作日期");
        cell8.setCellValue(
"申报日期");
        cell9.setCellValue(
"所属期起");
        cell10.setCellValue(
"所属期止");


    
for(int i= 0; i < taxpayerid.length; i++){
//定义数据从第二行开始       
  row 
= sheet.createRow((short) i+1);
                cell1 
= row.createCell((short) 0);
                cell2 
= row.createCell((short) 1);
                cell3 
= row.createCell((short) 2);
                cell4 
= row.createCell((short) 3);
                cell5 
= row.createCell((short) 4);
                cell6 
= row.createCell((short) 5);
                cell7 
= row.createCell((short) 6);
                cell8 
= row.createCell((short) 7);
                cell9 
= row.createCell((short) 8);
                cell10 
= row.createCell((short) 9);

               cell1.setEncoding((short) 
1);
               cell1.setCellType(
1);
               cell2.setEncoding((short) 
1);
               cell2.setCellType(
1);
               cell3.setEncoding((short) 
1);
               cell3.setCellType(
1);
               cell4.setEncoding((short) 
1);
               cell4.setCellType(
1);
               cell5.setEncoding((short) 
1);
               cell5.setCellType(
0);
               cell6.setEncoding((short) 
1);
               cell6.setCellType(
1);
               cell7.setEncoding((short) 
1);
               cell7.setCellType(
1);
               cell8.setEncoding((short) 
1);
               cell8.setCellType(
1);
               cell9.setEncoding((short) 
1);
               cell9.setCellType(
1);
               cell10.setEncoding((short) 
1);
               cell10.setCellType(
1);

//填充内容

        cell1.setCellValue(taxpayerid[i]);
        cell2.setCellValue(taxpayername[i]);
        cell3.setCellValue(tax[i]);
        cell4.setCellValue(f.parse(taxreduce[i].trim()).doubleValue());
        cell5.setCellValue(deratereasonname[i]);
        cell6.setCellValue(orgdeptname[i]);
        cell7.setCellValue(operatortime[i].substring(
0,16));
        cell8.setCellValue(declaredate[i].substring(
0,16));
        cell9.setCellValue(taxtermbegin[i].substring(
0,16));
        cell10.setCellValue(taxtermend[i].substring(
0,16));
    }
    wb.write(response.getOutputStream());
    response.getOutputStream().flush();
    response.getOutputStream().close();
%>


代码比较简单,首先把取得到的数据定义为一系列数组,然后定义表头,然后把取得的数据做为excel数据对应的放入,对poi有何疑问请参考http://java2.5341.com/3.html
posted on 2005-11-23 10:27 霹雳火 阅读(11670) 评论(12)  编辑  收藏

评论

# re: jsp导出excel 2006-02-15 08:37 ts
你们自己测过吗,报错:
Note: sun.tools.javac.Main has been deprecated.
/fdtj/test.jsp:11: Class org.apache.poi.hssf.usermodel.HSSFWorkbook not
found in import.
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
^
/fdtj/test.jsp:12: Class org.apache.poi.hssf.usermodel.HSSFSheet not found
in import.
import org.apache.poi.hssf.usermodel.HSSFSheet;
^
/fdtj/test.jsp:13: Class org.apache.poi.hssf.usermodel.HSSFRow not found
in import.
import org.apache.poi.hssf.usermodel.HSSFRow;
^
/fdtj/test.jsp:14: Class org.apache.poi.hssf.usermodel.HSSFCell not found
in import.
import org.apache.poi.hssf.usermodel.HSSFCell;
  回复  更多评论
  

# re: jsp导出excel 2006-02-15 11:00 霹雳火
如果我没测试过我就不会发上来,楼上的你自己没有导入包,这种应该算你的失误啊  回复  更多评论
  

# re: jsp导出excel 2006-03-08 14:39 请问,这是什么故障啊?
exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.NullPointerException
org.apache.jsp.test_jsp._jspService(org.apache.jsp.test_jsp:116)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

请问,这是什么故障啊?  回复  更多评论
  

# re: jsp导出excel 2006-03-10 14:24 霹雳火
NullPointerException 你要确保你的参数能从页面上取到值,所以必须为input,提交后要能由request得到,可能是你的没有得到值   回复  更多评论
  

# re: jsp导出excel 2006-07-10 21:26 jsp郁闷者
如何同时设置excel的纸张为横向?  回复  更多评论
  

# re: jsp导出excel 2008-01-02 10:16
哪可以下POI的JAR文件  回复  更多评论
  

# re: jsp导出excel 2008-04-07 00:32 BinTime
很好,找了好久了!  回复  更多评论
  

# re: jsp导出excel 2008-04-21 11:06 cfhjava
为什么我的是乱码了。<%@ page language="java" contentType="text/html;charset=utf-8"%>
我是JSP的。请帮!  回复  更多评论
  

# re: jsp导出excel[未登录] 2008-12-20 23:22 JAVA
OK,正在找....  回复  更多评论
  

# re: jsp导出excel 2008-12-28 00:51 mya
String[] taxtermend = request.getParameterValues("taxtermend");
请问右边这个taxtermend是页面上表格的ID还是什么名称?  回复  更多评论
  

# re: jsp导出excel 2009-05-31 08:42 guest
<%@ page language="java" contentType="text/html;charset=GBK"%>
这样就OK  回复  更多评论
  

# re: jsp导出excel 2009-06-08 11:47 文东
这个东西是怎么去调用的啊  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: