Javascript函数.xls到.csv

时间:2014-01-08 19:08:14

标签: javascript json

我正在使用IE 9.我的js功能适用于Chrome和IE,只是它不能在IE中以正确的格式保存文件。我想保存为.csv,在IE浏览器中,javascript只给我一个保存在.xls中的选项。 以下是我创建的函数:

downloadCSV: function(strData, strFileName){
            var strMimeType = "application/octet-stream";

            if (navigator.msSaveBlob) { // IE10
                return navigator.msSaveBlob(new Blob([strData], {type: strMimeType}), strFileName+".csv");
            }

            var a = Construct.create("a",{}, document.body);
            if ('download' in a) { //html5 A[download] FF and Chrome
                a.href = "data:" + strMimeType + "," + encodeURIComponent(strData);
                a.setAttribute("download", strFileName+".csv");
                a.innerHTML = "downloading...";
                setTimeout(function() {
                    a.click();
                    Construct.destroy(a);
                }, 66);
                return true;
            }

            var f = Construct.create("iframe",{}, document.body);
            if (typeof (window.ActiveXObject) == "undefined") {//old Chrome + FF
                f.src = "data:" + strMimeType + "," + encodeURIComponent(strData);
            } else {// IE 9
                var doc = f.contentWindow.document;
                doc.open("text/html");
                doc.charset = "utf-8";
                doc.write(strData);
                doc.close();
                doc.execCommand("SaveAs", null, strFileName+".csv");
            }
            setTimeout(function() {
                Construct.destroy(f);
            }, 333);
            return true;
        }

P.S。 IE9中启用了JavaScript

0 个答案:

没有答案