jquery.fileDownload.js插件导出excel

December 17, 2023
测试
测试
测试
测试
7 分钟阅读

因为使用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;
    }

继续阅读

更多来自我们博客的帖子

如何安装 BuddyPress
由 测试 December 17, 2023
经过差不多一年的开发,BuddyPress 这个基于 WordPress Mu 的 SNS 插件正式版终于发布了。BuddyPress...
阅读更多
Filter如何工作
由 测试 December 17, 2023
在 web.xml...
阅读更多
如何理解CGAffineTransform
由 测试 December 17, 2023
CGAffineTransform A structure for holding an affine transformation matrix. ...
阅读更多