[color=brown][size=small]使用SpringMvc导出csv文件时在火狐、谷歌、360、搜狗等浏览器都能正确导出,在ie浏览器上就提示找不到站点错误,本地ie版本是8.0.7601,错误提示如附件:[/size][/color]
[img]http://dl2.iteye.com/upload/attachment/0117/1354/203a22e0-a963-34df-9155-88568ef9f63b.png[/img]
[color=brown][size=small]程序代码如下:[/size][/color]
@RequestMapping(value="/exportWithdraw.do", method= RequestMethod.GET)
public ModelAndView exportDetail(HttpServletResponse res,ParmPojo pojo){
String fileName = new String("详情".getBytes("GBK"),"ISO8859-1")+".csv";
res.setContentType("application/vnd.ms-excel;charset=GBK");
res.setHeader("Content-Disposition", "attachment;filename="+fileName);
ModelAndView mv = new ModelAndView("/back/exportDetail.jsp");
List jsonObjList = bo.getByTch(pojo);
mv.addObject("datas", jsonObjList);//设置查询的结果集
return mv;
}
js页面调用
$("#tc_export").click(function(){
var params = getUrlParam();//获取参数
window.open(localPath + "exportWithdraw.do?" + params);
})
function getUrlParam() {
var condition = {};
condition.reportDateSch = reportDate;
condition.typeSch = type;
var html = [];
for(var key in condition){
html.push(key+'='+condition[key]);
}
return html.join('&');
}
导出文件的exportDetail.jsp页面内容
<%@page import="org.json.JSONObject"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%List jsonObjList = (List)request.getAttribute("datas");
StringBuilder sb = new StringBuilder();
for(JSONObject obj : jsonObjList) {
sb.append(obj.get("typeStr"));
sb.append(",").append(obj.get("realName"));
sb.append(",").append(obj.get("cityName"));
// 加上"\t"会显示后两位小数
sb.append(",\t").append(obj.get("balance"));
sb.append("\r\n");
}%>类型,真实姓名,城市,金额 <%=sb.toString()%>
经查找之后发现是请求响应没有重置导致,修改java代码如下:
@RequestMapping(value="/exportWithdraw.do", method= RequestMethod.GET)
public ModelAndView exportDetail(HttpServletResponse res,ParmPojo pojo){
String fileName = new String("详情".getBytes("GBK"),"ISO8859-1")+".csv";
res.reset();//解决ie浏览器下载找不到站点问题
res.setContentType("application/vnd.ms-excel;charset=GBK");
res.setHeader("Content-Disposition", "attachment;filename="+fileName);
ModelAndView mv = new ModelAndView("/back/exportDetail.jsp");
List jsonObjList = bo.getByTch(pojo);
mv.addObject("datas", jsonObjList);//设置查询的结果集
return mv;
}
[b][color=red] 增加了 res.reset(); ,问题解决了,所有浏览器都支持[/color][/b]