qiang
qiang

Reputation: 7

Use js export excel, tens of thousands of data, what should I do?

I've used the following method, but it doesn't fit

export = (function() {
             var uri = 'data:application/vnd.ms-excel;base64,'
                , template = '<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"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
                , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) ;}
                , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }); };
        return function(table, name,filename) {
            if (!table.nodeType) table = document.getElementById(table);
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
            // window.location.href = uri + base64(format(template, ctx));
            document.getElementById("dlink").href = uri + base64(format(template, ctx));
            document.getElementById("dlink").download = "finame";
            document.getElementById("dlink").click();
        };
    })();

Upvotes: 0

Views: 127

Answers (1)

DarkMukke
DarkMukke

Reputation: 2489

You might want create the file on the server and serve it via an AJAX call or a websocket.

The main problem is that the client has to parse all that data, and it might be quite slow/heavy depending on the client.

Also remember if your table is paginated you will need to select every page manually to parse the table into 1 big result set.

As of the row limitations, according to Excel's Specifications, from Excel 2007 and upwards you should have 1,048,576 rows per workbook.

Upvotes: 1

Related Questions