在IE 11中下载Excel文件

时间:2016-09-30 08:35:10

标签: javascript excel internet-explorer download extjs4.1

我有这个ExtJS代码,用于将网格数据转换为Excel格式并写入文件,代码最终将网格数据导出为可下载的Excel文件。这在firefox和chrome中运行良好但在IE浏览器(IE 11)中无法运行。

在分析时发现代码最终会在字符串下面发布到URL框中,该框将下载excel文件。

data:application/vnd.ms-excel;base64,<base64 encoded string follows here>

起初,我认为问题可能在代码中的某个地方,然后我碰巧检查下面的步骤。 如果我在chrome或firefox中粘贴下面的内容,它会提示下载文件并下载一个虚拟文件。

data:application/vnd.ms-excel;base64,blahblahblahblahblah

但是如果我在IE 11中粘贴上面相同的字符串,它什么都不做。所以我假设代码工作正常,但我可能需要做更多的事情,以使其兼容IE浏览器。但我不知道我还能尝试什么。

非常感谢任何指针。

如果有帮助,

下面是我的ExtJS代码。

    //here xmlString has the constructed xml string which holds 
    //grid column headers + grid rows with Table, Column, Cell and Worksheet tags.

    var ctx = {
        created : (new Date()).getTime(),
        worksheets : xmlString
    };

    var uri = 'data:application/vnd.ms-excel;base64,',
    tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">' + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>BlahBlah</Author><Created>{created}</Created></DocumentProperties>' + '<Styles>' + '<Style ss:ID="columnHeader"><Font ss:Size="11" ss:Bold="1" /><Interior ss:Color="#CCFFCC" ss:Pattern="Solid"/> </Style>' + '</Styles>' + '{worksheets}</Workbook>',
        base64 = function(s) {
             return window.btoa(unescape(encodeURIComponent(s)));
    },
        format = function(s, c) {
        return s.replace(/{(\w+)}/g, function(m, p) {
            return c[p];
        });
    };

    var workbookXML = format(tmplWorkbookXML, ctx);
    var link = document.createElement("A");
    link.href = uri + base64(workbookXML);
    link.download = sheetName + " Level Report - " + Ext.Date.format(new Date(), 'Y-m-d H:i:s') + '.xls';
    link.target = '_blank';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);

PS:我在ExtJS V4.1和V6.0上都尝试过这种方法但行为相同。

0 个答案:

没有答案