rainabba / jquery-table2excel

jQuery Plugin to export HTML tabled to Excel Spreadsheet Compatible Files
593 stars 666 forks source link

Content coming as {table0} #12

Open Ramakishore opened 9 years ago

Ramakishore commented 9 years ago

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

avinav commented 9 years ago

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

that worked for me.

rainabba commented 9 years ago

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.

rabbitkun commented 9 years ago

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

avinav commented 9 years ago

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

julwong commented 9 years ago

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)

xenon75 commented 9 years ago

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 commented 9 years ago

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 }

shuaixieca commented 8 years ago

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

gioce90 commented 8 years ago

This problem still persist???

FamkeSC commented 8 years ago

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?

gioce90 commented 8 years ago

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

FamkeSC commented 8 years ago

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

gioce90 commented 8 years ago

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

FamkeSC commented 8 years ago

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 commented 8 years ago

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 );
gioce90 commented 8 years ago

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