我想实现将查询得到的值全部导出到excel
大概意思是这样的:
我的一个jsp页面 getAttribute得到一个List
然后新增一个按钮,点击按钮就可以将这些值导出到excel上网找资料找到一个写法:
这个是下载(应该是)
<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*,com.direct.query.ps.WriteExcel" pageEncoding="GBK"%><%
response.setHeader("Content-Disposition","attachment;filename=test123.xls");//指定下载的文件名
response.setContentType("application/vnd.ms-excel"); 
WriteExcel  we=new WriteExcel();
we.getExcel("111.xls",response.getOutputStream());
%>
下面为手动创建EXCEL的类public   void   getExcel(String   sheetName,OutputStream   output) 
 {  
 HSSFWorkbook wb=new HSSFWorkbook();
 HSSFSheet sheet=wb.createSheet("sheet1");
 HSSFRow cell1 = sheet.createRow((short)0); 
 cell1.createCell((short)0).setCellValue("清单");
 
 HSSFRow cell2 = sheet.createRow((short)1); 
 cell2.createCell((short)0).setCellValue("、名称");
     try   { 
         output.flush(); 
         wb.write(output); 
         output.close();
 }   catch   (IOException   e)   { 
         e.printStackTrace(); 
         System.out.println( "Output   is   closed "); 
 } 
现在我的问题是如何把我前台的list放入excel?
也就是说我如何才能将数据库里面的值导出到excel?
请前辈们给点意见!先谢谢了!!

解决方案 »

  1.   

    可以在action中得到你的list然后用POI生成EXCEL文件,关于po在网上查一下就会用了
      

  2.   


    呵呵,在action中如何得到我的list啊!
      

  3.   

    新增一个按钮,点击按钮就可以将这些值导出到excel点击那个按钮的时候 就进入到action里面查询 这次查询之后直接跳转到新的jsp页面,可以复制
    前面那个,然后在新的jsp里面加上这段话即可:
    java.text.SimpleDateFormat tempDate = new java.text.SimpleDateFormat(
    "yyyyMMddHHmmss");
    String time = tempDate.format(new Date());
    String fileName = "用户基本信息_" + time + ".xls";
    fileName = new String(fileName.getBytes("GBK"), "ISO8859_1");
    response.setHeader("Content-Disposition", "attachment;filename="
    + fileName); response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");看你具体的需求吧 这是大概的思路
      

  4.   

    噢  还有这句 房子head里面的
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    防止导出之后中文乱码的
      

  5.   

    呵呵,问题解决了!我在那个getexcel方法里面加了个参数,把我页面的list用session传过去!
    谢谢大家了!
      

  6.   

    添加属性 public InputStream in