Java 如何通过ajax下载xls文件

Java 如何通过ajax下载xls文件,java,jquery,ajax,xls,vraptor,Java,Jquery,Ajax,Xls,Vraptor,我正在通过Ajax和VRaptor直接下载xls,VRaptor使您的论文更直观,并提供如下代码的文件: public Download geraExcel() { File arquivo = //generate file return new FileDownload(file, "MyFile.xls", "application/vnd.ms-excel", true); } 问题是在ajax的成功中接收到这个文件,我有一个这样做的代码,它对txt非常有效,问题是xls打开时损

我正在通过Ajax和VRaptor直接下载xls,VRaptor使您的论文更直观,并提供如下代码的文件:

public Download geraExcel() {
 File arquivo = //generate file 
 return new FileDownload(file, "MyFile.xls", "application/vnd.ms-excel", true);
}
问题是在ajax的成功中接收到这个文件,我有一个这样做的代码,它对txt非常有效,问题是xls打开时损坏了,我花了很长时间研究解决方案,都没有成功,持续的文件损坏了。如果有人知道另一种方法,我感谢你。Ajax.success下面的代码:

success: function(response, status, xhr) {
    // check for a filename
    var filename = "";
    var disposition = xhr.getResponseHeader('Content-Disposition');
    if (disposition && disposition.indexOf('attachment') !== -1) {
        var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
        var matches = filenameRegex.exec(disposition);
        if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
    }

    var type = xhr.getResponseHeader('Content-Type');
    var blob = new Blob([response], { type: type });

    if (typeof window.navigator.msSaveBlob !== 'undefined') {
        // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
        window.navigator.msSaveBlob(blob, filename);
    } else {
        var URL = window.URL || window.webkitURL;
        var downloadUrl = URL.createObjectURL(blob);

        if (filename) {
            // use HTML5 a[download] attribute to specify filename
            var a = document.createElement("a");
            // safari doesn't support this yet
            if (typeof a.download === 'undefined') {                
                window.location = downloadUrl;
            } else {
                a.href = downloadUrl;
                a.download = filename;
                document.body.appendChild(a);
                a.click();

                //Here is the problem, the original is about 15k,  
                // but the download file is about 26K

            }
        } else {                    
            window.location = downloadUrl;
        }

        setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
    }
}

//生成文件
?您是否正在生成XLS文件或CSV/HTML,并将内容类型设置为在Excel中打开?我正在生成XLS文件。内容类型:application/vnd.ms-excel您可以生成一个HTML表格,并将内容设置为
application/vnd.ms excel
,它将在excel中正常打开。但您会收到一条“此文件可能已损坏”的消息,这并不理想,因为我已经用java完成了这一代,不想重做