枕上从妨一夜睡,灯前读尽十年诗。这篇文章主要讲述jquery.fileDownload.js插件导出excel相关的知识,希望能为你提供帮助。
【jquery.fileDownload.js插件导出excel】因为使用ajax导出excel会出现问题,所以现在使用jQuery.fileDownload.js插件来解决导出excel的问题
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
在页面引入jquery.fileDownload.js插件
1、如下所示
< script type="text/javascript" src="https://www.songbingjia.com/android/${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源码解析:
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); }
2、在后台代码中设置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"));
//在这里加入设置Cookie -------------
Cookie fileDownload=new Cookie("fileDownload", "true");
fileDownload.setPath("/");
response.addCookie(fileDownload);
//------------------------------------
ServletOutputStream out = response.getOutputStream();
final HSSFWorkbook workbook = new HSSFWorkbook();
List< Future< Boolean> > resultList = new ArrayList< Future< Boolean> > ();
3、如果要使回调函数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; }
推荐阅读
- 配置思科路由器Telnet 登陆
- Word中使用正则表达式进行查找和替换与难题征解
- Powershell 创建炫丽美观的Html报表
- Hadoop的word co-occurrence实现
- Powershell AWS 自动化管理 (11) - 创建一个高可用的WordPress博客(中)
- 异步陷阱之IO篇
- 解决Excel打开UTF-8编码的CSV文件乱码的问题
- PageOffice 使用Dome
- 解决PPTP错误(ppp:compressor dropped pkt)