用技术改变命运

积累 沉淀
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

java导出 csv文件以及java导出office文件

Posted on 2012-02-02 15:51  远航的兵  阅读(202)  评论(0)    收藏  举报

导出类似下载,

   response.setContentType("application/download;charset=GBK");   

   response.setHeader("Content-disposition","attachment;filename=\"" +new String("授信用户行为报表(月末欠费金额)".getBytes("GBK"),"ISO-8859-1")+new Date().toLocaleString().substring(0,10)+".csv\"");  

    OutputStream o = response.getOutputStream();  

  //解决异常java.lang.IllegalStateException: getOutputStream() has already been called for this response  

 /**    * java.lang.IllegalStateException: getOutputStream() has already been called for this respons  

原因:是web容器生成的servlet代码中有out.write(""),这个和JSP中1.调用的response.getOutputStream()产生冲突.即Servlet规范说明,

 不能既调用response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个, 在调用第二个时候应会抛出IllegalStateException,因为在jsp中,out变量实际上是通过response.getWriter得到的,你的程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。  

   */ //  PrintWriter out = response.getWriter(); //  out.flush(); //  out.close();

 

参考:

http://www.blogjava.net/DreamAngel/archive/2011/12/23/367050.html