内容简介: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="${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", "attachment;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; }
以上所述就是小编给大家介绍的《jquery.fileDownload.js插件导出excel》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- VLOOK 10.3 发布!带来了导航中心、一键导出集成~好用实用 Typora/Markdown 插件
- vue+express图片上传并利用js-xlxs插件将图片链接导出到excel
- ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程
- MrDoc 0.5.0 版本发布,优化 EPUB 导出,新增 PDF 导出,支持自定义思维导图
- php 导出 excel
- oracle导出序列sequence
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。