fileName = new String(fileName.getBytes("GBK"),"iso8859-1"); //转码
response.setContentType("application/vnd.ms-excel");
 //response.setContentType( "APPLICATION/OCTET-STREAM" );
response.addHeader("Content-Disposition", "attachment; filename="+fileName+".xls");
workbook.write(response.getOutputStream());response.getOutputStream().flush();
response.getOutputStream().close();

这2行放到最后试看看。

解决方案 »

  1.   

    楼上正确
    不过我用楼主的代码测试也没抛出java.lang.IllegalStateException异常,只是在打开excel文件时报“不能打开文档”fileName = new String(fileName.getBytes("GBK"),"iso8859-1"); //转码response.setContentType("application/vnd.ms-excel");
    //response.setContentType( "APPLICATION/OCTET-STREAM" );
    response.addHeader("Content-Disposition", "attachment; filename="+fileName+".xls");
    response.getOutputStream().flush();
    response.getOutputStream().close();workbook.write(response.getOutputStream());
      

  2.   

    问题在于,你不要在 jsp 里面生成 excel,而应该在 servlet 里面做!
      

  3.   

    和生成图片类似,都是输出到OutputStream, 请参照我的图片程序http://blog.csdn.net/java2000_net/archive/2007/12/21/1956716.aspx主要是后面的servlet影射成jsp的部分