由於使用ajax導出excel會出現問題,因此如今使用jQuery.fileDownload.js插件來解決導出excel的問題javascript
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
html
在頁面引入jquery.fileDownload.js插件java
一、以下所示jquery
<script type="text/JavaScript" src="${resource}/js/jquery.fileDownload.js"/></script> <script type="text/javascript"> $("#export_confirm").on("click",function(){ var url="${path}/admin/information/student/export"; $.fileDownload(url,{ data:{semesterId:$("#misSemester").val()}, prepareCallback:function(url){ alert("正在導出,請稍後..."); }, successCallback: function(url){ alert("導出完成!"); }, failCallback: function (html, url) { alert("導出失敗,未知的異常。"); } }); });
jquery-file-Download.js源碼解析:ajax
onPrepare: function (url) { //preparingMessageHtml屬性存在,彈出導出準備提示框 if (settings.preparingMessageHtml) { //jueryUi dialog 可本身修改爲其它的。 $preparingDialog = $("<div>").html(settings.preparingMessageHtml).dialog(settings.dialogOptions); } else if (settings.prepareCallback) { //調用回調函數 settings.prepareCallback(url); } }, //導出失敗調用的函數 onFail: function (responseHtml, url, error) { //準備提示對話框存在則關閉 if ($preparingDialog) { $preparingDialog.dialog('close'); } //failMessageHtml存在彈出對話框提示 if (settings.failMessageHtml) { $("<div>").html(settings.failMessageHtml).dialog(settings.dialogOptions); } //調用回調函數 settings.failCallback(responseHtml, url, error); deferred.reject(responseHtml, url); }
二、在後臺代碼中設置Cookie,並返回Cookie的值cookie
public void export(final HttpServletRequest request, HttpServletResponse response,final String semesterId) throws IOException, IllegalArgumentException, IllegalAccessException {
String fileName = "excel文件";
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "p_w_upload;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));app
//在這裏加入設置Cookie -------------
Cookie fileDownload=new Cookie("fileDownload", "true");
fileDownload.setPath("/");
response.addCookie(fileDownload);dom
//------------------------------------
ServletOutputStream out = response.getOutputStream();
final HSSFWorkbook workbook = new HSSFWorkbook();
List<Future<Boolean>> resultList = new ArrayList<Future<Boolean>>();ide
三、若是要使回調函數successCallback和failCallback起做用,還得在後臺代碼中返回Cookie函數
jquery-file-Download.js源碼解析:
後臺設置與特定的cookie值
前臺js定時去調用checkFileDownloadComplete方法,檢查前臺與後臺返回的cookie值是否匹配
//check if the file download has completed every checkInterval ms setTimeout(checkFileDownloadComplete, settings.checkInterval); function checkFileDownloadComplete() { //has the cookie been written due to a file download occuring? var cookieValue = settings.cookieValue; if(typeof cookieValue == 'string') { cookieValue = cookieValue.toLowerCase(); } var lowerCaseCookie = settings.cookieName.toLowerCase() + "=" + cookieValue; if (document.cookie.toLowerCase().indexOf(lowerCaseCookie) > -1) { //execute specified callback internalCallbacks.onSuccess(fileUrl); //remove cookie var cookieData = settings.cookieName + "=; path=" + settings.cookiePath + "; expires=" + new Date(0).toUTCString() + ";"; if (settings.cookieDomain) cookieData += " domain=" + settings.cookieDomain + ";"; document.cookie = cookieData; //remove iframe cleanUp(false); return; }