Excel Export无法使用Google Chrome浏览器记录很多 [英] Excel Export Not Working Google Chrome for Lots of Records

查看:163
本文介绍了Excel Export无法使用Google Chrome浏览器记录很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java脚本或jquery将html表数据导出到excel,与所有浏览器兼容.我正在使用以下脚本

I want to export the html table data to excel using either java script or jquery, comatibale with all browser. I am using below script

        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浏览器中测试相同的脚本时,它无法正常工作.我收到网络错误消息 我在哪里犯错.你能帮我吗?

For lots of records this script works fine in Mozilla Firefox, But when I tested same script in Chrome browser it is not working .I am getting network error Where do I make mistakes. Could you help me?

推荐答案

在excel导出过程中,如果记录太多,则崩溃的原因超出了Chrome的网址限制.这是Chrome的错误.

During excel export, if there are too many records, the reason for the crash is exceeding the chrome's url limit. This is a bug of chrome.

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

In the following codes, the data is converted to a blob URL and exported. My problem solved. I wish this is also useful to others.

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;
    }

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

Now we can use function like below.

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);

这篇关于Excel Export无法使用Google Chrome浏览器记录很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆