程序代码如下:
@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<JSONObject> 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<JSONObject> jsonObjList = (List<JSONObject>)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<JSONObject> jsonObjList = bo.getByTch(pojo); mv.addObject("datas", jsonObjList);//设置查询的结果集 return mv; }
增加了 res.reset(); ,问题解决了,所有浏览器都支持