public String excel() throws Exception{
initRequest();
//查询ID
String queryId = request.getParameter("queryId");
RequestParamters requestParamters = new RequestParamters(request, false);
HSSFWorkbook wb = null;
try {
wb = publicSrv.excel(queryId, getSqlColumns(queryId), requestParamters);
} catch (Exception ex) {
ex.printStackTrace();
}
HttpServletResponse response = getResponse();
OutputStream os = null;
try {
QueryConfig queryConfig = ConfigQuery.getQueryConfig(queryId);
String title = null;
if(null != queryConfig){
title = queryConfig.getExcelTitle();
}
if(StrUtils.isEmpty(title)){
title = "无标题";
}
os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition","attachment;filename="+new String((title).getBytes(),"ISO8859_1")+".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
os.flush();
os.close();
} catch (IOException e) {}
}
return null;
}
如果action的返回写成
return SUCCESS;
在tomcat6.0.14的
版本会出现
getOutputStream() has already been called for this response
,在tomcat6.0.28中没有,解决办法是action最后必须是
return null;