struts2+poi导出excel出错
做struts2+poi导出excel时,发生如下错误,不知怎么解决,请各位指教:
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:462) at org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler.sendErrorResponse(DefaultDispatcherErrorHandler.java:80) at org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler.handleError(DefaultDispatcherErrorHandler.java:59) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:909) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:576) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)struts.xml文件
<!-- 导出excel --> <action name="export_*" class="org.fkjava.s2sh.action.ExportExcelAction"> <result name="success" type="stream"> <!-- 指定二进制流的类型 --> <param name="contentType">${fileType}</param> <!-- 默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法, 比如这里就是 getExcelInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 --> <param name="inputName">is</param> <param name="contentDisposition"> attachment;filename=${fileName}</param> <!-- 输出时缓冲区的大小 --> <param name="bufferSize">4096</param> </result> </action>action
package org.fkjava.s2sh.action; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; import org.fkjava.s2sh.domain.User; import org.fkjava.s2sh.service.UserService; import org.fkjava.s2sh.util.ExcelUtil; import com.opensymphony.xwork2.Action; public class ExportExcelAction implements Action { private UserService us; private List<User> users; private String fileType; private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public List<User> getUsers() { return users; } public void setUsres(List<User> users) { this.users = users; } public void setUs(UserService us) { this.us = us; } public InputStream getIs() { users = us.getAllUser(null); ByteArrayOutputStream out = null; if (fileType.equals("application/vnd.ms-excel")) { out = new ByteArrayOutputStream(); ExcelUtil.toExcel(users, out); return new ByteArrayInputStream(out.toByteArray()); } return null; } @Override public String execute() throws Exception { return SUCCESS; } }jsp页面
<a href="${pageContext.request.contextPath }/export_excel?fileType=application/vnd.ms-excel&fileName=userInfo.xls">导出Excel报表</a>ExcelUtil.java
package org.fkjava.s2sh.util; import java.io.OutputStream; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.fkjava.s2sh.domain.User; /** * excel导出工具类 * * @author 杨仔 * @Description TODO * @ClassName ExcelUtil * @date 2015年10月10日 * @version 1.0 * */ public class ExcelUtil { public static void toExcel(List<User> users, OutputStream out) { String[] title = { "姓名", "密码", "生日" }; try { HSSFWorkbook wwb = new HSSFWorkbook(); // 创建工作簿 HSSFSheet sheet = wwb.createSheet("教学档案"); HSSFRow row = null; // 创建表头 row = sheet.createRow(0); for (int i = 0; i < title.length; i++) { row.createCell(i).setCellValue(title[i]); } // 添加内容 User user = null; for (int i = 0; i < users.size(); i++) { user = users.get(i); row = sheet.createRow(i + 1); // 列 row.createCell(0).setCellValue(user.getName()); row.createCell(1).setCellValue(user.getPass()); row.createCell(2).setCellValue(user.getBirth()); } HSSFDataFormat format = wwb.createDataFormat(); // 格式化数据 HSSFCellStyle style = wwb.createCellStyle(); style.setDataFormat(format.getFormat("yyyy-MM-dd hh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
getIs方法响应了两次,就报这个错,不知怎么解决
就是你说的struts响应OutputStream这部分,但我不知道怎么改
回复
struts了解不是很深,帮不到你了。 两次响应这种问题应该是常见问题吧?
就是不知道怎么解决这种两次响应问题
回复
http://bbs.csdn.net/topics/370053062,借鉴一下吧。
谢谢啊
表面上看是 response 时,出现问题了
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
深层次的问题需要确定你
ExcelUtil.toExcel(users, out);
方法是否正确工作了,如果这句话正确并且产生的 excel 没问题。那么重点就落在你 struts 响应 OutputStream 这部分吧。
刚开始把contentType在struts.xml中写死是可以的,我后来改成$(fileType)就报错了了,
jsp页面问题,导出excel要用把参数以form的形式提交。