Excel导出不适用于Google Chrome的大量记录

时间:2017-12-08 06:37:50

标签: javascript jquery excel google-chrome

我想使用java脚本或jquery,comatibale和所有浏览器将html表数据导出到excel。我正在使用下面的脚本

        tableToExcel: function (table, name, sheetName) {
        var e = this, fullTemplate = "", i, link, a;

        e.uri = "data:application/vnd.ms-excel;base64,";
        e.base64 = function (s) {
            return window.btoa(unescape(encodeURIComponent(s)));
        };
        e.format = function (s, c) {
            return s.replace(/{(\w+)}/g, function (m, p) {
                return c[p];
            });
        };

        sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName;

        e.ctx = {
            worksheet: name || "Worksheet",
            table: table,
            sheetName: sheetName,
        };

        fullTemplate = e.template.head;

        if ($.isArray(table)) {
            for (i in table) {
                fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail;
            }
        }

        fullTemplate += e.template.mid;

        if ($.isArray(table)) {
            for (i in table) {
                fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
            }
        }

        fullTemplate += e.template.foot;

        for (i in table) {
            e.ctx["table" + i] = table[i];
        }
        delete e.ctx.table;

        if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
        {
            if (typeof Blob !== "undefined") {
                //use blobs if we can
                fullTemplate = [fullTemplate];
                //convert to array
                var blob1 = new Blob(fullTemplate, { type: "text/html" });
                window.navigator.msSaveBlob(blob1, getFileName(e.settings));
            } else {
                //otherwise use the iframe and save
                //requires a blank iframe on page called txtArea1
                txtArea1.document.open("text/html", "replace");
                txtArea1.document.write(e.format(fullTemplate, e.ctx));
                txtArea1.document.close();
                txtArea1.focus();
                sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings));
            }

        } else {
            link = e.uri + e.base64(e.format(fullTemplate, e.ctx));
            a = document.createElement("a");
            a.download = getFileName(e.settings);
            a.href = link;

            document.body.appendChild(a);

            a.click();

            document.body.removeChild(a);
        }

        return true;
    }

对于大量记录,此脚本在Mozilla Firefox中运行良好,但是当我在Chrome浏览器中测试相同的脚本时,它无法正常工作。我收到网络错误 我在哪里犯错误。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

在excel导出期间,如果记录太多,则崩溃的原因超出了chrome的url限制。这是一个铬的错误。

在以下代码中,数据将转换为blob URL并导出。我的问题解决了。我希望这对其他人也有用。

function b64toBlob(b64Data, contentType, sliceSize) {
            contentType = contentType || '';
            sliceSize = sliceSize || 512;

            var byteCharacters = atob(b64Data);
            var byteArrays = [];

            for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                var slice = byteCharacters.slice(offset, offset + sliceSize);

                var byteNumbers = new Array(slice.length);
                for (var i = 0; i < slice.length; i++) {
                    byteNumbers[i] = slice.charCodeAt(i);
                }

                var byteArray = new Uint8Array(byteNumbers);

                byteArrays.push(byteArray);
            }

            var blob = new Blob(byteArrays, { type: contentType });
            return blob;
    }

现在我们可以使用如下功能。

var blob = b64toBlob(e.base64(e.format(fullTemplate, e.ctx)), "application/vnd.ms-excel");
var blobUrl = URL.createObjectURL(blob);
a = document.createElement("a");
a.download = getFileName(e.settings);
a.href = blobUrl;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);