关于java导出excel的问题

Spring89 2010-10-07 07:19:04
java导出excel(用的是poi jar包),出现以下错误,下载对话框没有弹出来,请问,这是什么原因?怎样改?

2010-10-7 19:12:38 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:610)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:178)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
at org.apache.jsp.user_005flist_jsp._jspService(user_005flist_jsp.java:712)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
......

BaseAction代码如下(Struts2+Hibernate+Spring):
protected void downloadExcel(HSSFWorkbook workbook, HttpServletResponse response, String filename)
throws IOException {

OutputStream out = response.getOutputStream();

response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
workbook.write(out);

//out.flush();
out.close();

}

Action代码如下:
public String exportExcel() throws Exception {
UserInfoVO user = new UserInfoVO();
List list = userInfoService.exportUser(user);

// 填充数据
HSSFWorkbook workbook = this.fillDataToExcel(list);

Date date = new Date();
String currentDate = DateFormatUtil.getStringToDate(date, "yyyyMMdd");

if(null != workbook){
this.downloadExcel(workbook, this.getResponse(), "用户信息("+currentDate+").xls");
}
return "list";
}
...全文
131 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
耿直派 2010-10-08
解释下(不是很专业),我记得struts2下载时需要获取一个inputstream,在action里面return它,客户端就可以下载了(struts.xml里面还有相关配置),楼主的代码看起来像是下载excel,就把work这个东东放到一个inputstream里面然后"返回"它,不知道对楼主有帮助没
回复
耿直派 2010-10-08
个人理解哈,OutputStream out = response.getOutputStream();这个和Jsp的out冲突,这个问题比较明显,和poi无关,呵呵;
楼主可以这样试试:ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos);byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-07 07:19
社区公告
暂无公告