引入js;
js代碼:
var url = "${pageContext.request.contextPath}/exportExcel/exportMemberFile"; //實現Ajax下載文件 $.fileDownload(url, { httpMethod : 'POST', data : $("#form1").serialize(), prepareCallback : function(url) {debugger $("#exportBtn").attr("disabled", true); $("#exportBtn").html("正在導出"); }, successCallback : function(url) {debugger $("#exportBtn").attr("disabled", false); $("#exportBtn").html("導出"); }, failCallback : function(html, url) { alert("導出失敗"); } });
后台response要進行如下設置;
response.setContentType("application/x-download"); response.setCharacterEncoding("utf-8");// 處理編碼問題 response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xls");// 表頭編碼問題 Cookie fileDownload=new Cookie("fileDownload", "true"); fileDownload.setPath("/"); response.addCookie(fileDownload);
然后就可以用了