jquery-table2excel icon indicating copy to clipboard operation
jquery-table2excel copied to clipboard

Content coming as {table0}

Open Ramakishore opened this issue 10 years ago • 16 comments
trafficstars

Hi Guys,

After referring the latest Js file I am not able to export any of my content. Exported content is coming as {table0} in excel sheet. Could you please let me know the correct JS reference ?

Regards, Rama

Ramakishore avatar May 05 '15 04:05 Ramakishore

In src/jquery.table2excel.js change: txtArea1.document.write(fullTemplate) to txtArea1.document.write(e.format(fullTemplate,e.ctx))

that worked for me.

avinav avatar May 21 '15 20:05 avinav

Looks like you're referring to line 109. If so, I just did a hot commit to master to update that. Need to re-generate minified version still though. Will close after I do that and someone confirms the fix.

rainabba avatar May 21 '15 20:05 rainabba

I'm having this kind of error in IE :|

rabbitkun avatar Jul 01 '15 07:07 rabbitkun

Do you have the latest version? Check this file: src/jquery.table2excel.js and compare with one in repository. line 109 should be txtArea1.document.write(e.format(fullTemplate, e.ctx));

avinav avatar Jul 01 '15 07:07 avinav

This fix for my problem on IE 11: insert a new line fullTemplate = e.format(fullTemplate, e.ctx); after if (typeof Blob !== "undefined") { (line 99)

julwong avatar Jul 23 '15 09:07 julwong

i have problem too. solved as JulH said, insert new line

if (typeof Blob !== "undefined") { //use blobs if we can
fullTemplate = e.format(fullTemplate, e.ctx); // <<< insert this line!!! fullTemplate = [fullTemplate]; //convert to array var blob1 = new Blob(fullTemplate, { type: "text/html" }); window.navigator.msSaveBlob(blob1, getFileName(e.settings)); } else { ..... }

xenon75 avatar Aug 12 '15 03:08 xenon75

excel extention name 'xlsx' worked not for me so, i rewrite xlsx to xls

function getFileName(settings) { return (settings.filename ? settings.filename : "table2excel") + ".xls"; // <-- change xlsx to xls }

xenon75 avatar Aug 12 '15 03:08 xenon75

Anyone knows how to fix the {table0} for worksheet name?

shuaixieca avatar Jan 14 '16 20:01 shuaixieca

This problem still persist???

gioce90 avatar Jun 28 '16 08:06 gioce90

I have the latest version of table2excel installed, but have the same problem. It does work in both Chrome and Firefox, but I get the same {table0} in IE 11. Does anyone know what to do?

FamkeSC avatar Sep 06 '16 13:09 FamkeSC

Yes, I have fixed this, now you can use IE10 and 11... but you have to download the .js file from src folder... not the dist folder ;)

gioce90 avatar Sep 06 '16 14:09 gioce90

When I do that, my whole page freezes when I click the download button.

Could it have something to do with the jQueryversion I'm using? It's version 2.1.4

FamkeSC avatar Sep 06 '16 14:09 FamkeSC

can you debug the src/jquery.table2excel.js file?

gioce90 avatar Sep 07 '16 09:09 gioce90

I can't, the page freezes before any error messages pop up.

When I use an older version of table2excel, it works in Chrome and Firefox, but not in Internet Explorer. The older version I use has this part (I've understood that that is the most important part?):

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

FamkeSC avatar Sep 08 '16 10:09 FamkeSC

I fixed the bug by using an older version of Table2Excel (I'm not sure anymore where I found it), and, as it says above, editing the if (typeOf blob !== "undefined")-part. I have no idea why that did not work before, but hey, it works now! My screen won't freeze because I'm not using the newer version, and it does work in Internet Explorer.

This is the code I used:

//table2excel.js
;(function ( $, window, document, undefined ) {
    var pluginName = "table2excel",

    defaults = {
        exclude: ".noExl",
                name: "Table2Excel"
    };

    // The actual plugin constructor
    function Plugin ( element, options ) {
            this.element = element;
            // jQuery has an extend method which merges the contents of two or
            // more objects, storing the result in the first object. The first object
            // is generally empty as we don't want to alter the default options for
            // future instances of the plugin
            //
            this.settings = $.extend( {}, defaults, options );
            this._defaults = defaults;
            this._name = pluginName;
            this.init();
    }

    Plugin.prototype = {
        init: function () {
            var e = this;

            var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">";
            e.template = {
                head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">" + utf8Heading + "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
                sheet: {
                    head: "<x:ExcelWorksheet><x:Name>",
                    tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
                },
                mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
                table: {
                    head: "<table>",
                    tail: "</table>"
                },
                foot: "</body></html>"
            };

            e.tableRows = [];

            // get contents of table except for exclude
            $(e.element).each( function(i,o) {
                var tempRows = "";
                $(o).find("tr").not(e.settings.exclude).each(function (i,o) {
                    tempRows += "<tr>" + $(o).html() + "</tr>";
                });
                e.tableRows.push(tempRows);
            });

            e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName);
        },

        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 + "{worksheet" + i + "}" + e.template.sheet.tail;
                      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 = e.format(fullTemplate, e.ctx); // <<< insert this line!!!
                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;
        }
    };

    function getFileName(settings) {
        return ( settings.filename ? settings.filename : "table2excel" ) +
               ( settings.fileext ? settings.fileext : ".xls" );
    }

    $.fn[ pluginName ] = function ( options ) {
        var e = this;
            e.each(function() {
                if ( !$.data( e, "plugin_" + pluginName ) ) {
                    $.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
                }
            });

        // chain jQuery functions
        return e;
    };

})( jQuery, window, document );

FamkeSC avatar Sep 12 '16 15:09 FamkeSC

This works with IE11.... Have you tried with IE 9 and 10?

gioce90 avatar Sep 12 '16 16:09 gioce90