jquery.fileDownload.js插件导出excel

时间:2022-06-18
本文章向大家介绍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", "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;
    }