0

I'm new to javascript and I have an existing code:

var uri = {excel: 'data:application/vnd.ms-excel;base64,', csv: 'data:application/csv;base64,'};

var base64 = function(s) {
    return window.btoa(window.unescape(encodeURIComponent(s)));
};

var format = function(s, c) {
    return s.replace(new RegExp("{(\\w+)}", "g"), function(m, p) {
        return c[p];
    });
};

excel: function(anchor, table, name) {
     table = get(table);
     var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
     var hrefvalue = base64(format(template.excel, ctx));
     anchor.href = hrefvalue;
     // Return true to allow the link to work
     return true;
 }

This returns "data:application/vnd.ms-excel;base64,sdsssapqPphgrtG1ks...." which is b64 data. It works if the records I'm trying to export is less than 2000 but does not work if more than 2000 records.

I modified it by converting the data to blob:

   excel: function(anchor, table, name) {
        table = get(table);
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
        var hrefvalue = base64(format(template.excel, ctx));
        var blob = b64toBlob(hrefvalue, "application/vnd.ms-excel");
        var blobUrl = window.URL.createObjectURL(blob);
        window.location = blobUrl;
        // Return true to allow the link to work
        return true;
    },

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;

}

But it returns "blob:http://localhost:8888/97e93f9a-51ea-4480-9434-634061497e3f" which is different from the current output. I can now export more than 2000 records but in the spreadsheet it also contains the elements name of my webpage (button, menu, textbox) plus it downloads an extra file with a naming convention of 97e93f9a-51ea-4480-9434-634061497e3f. opening the file contains the html format of the records.

How is it downloading an extra file? and how to properly create the spreadsheet to omit elements from my webpage?

Arnold Cristobal
  • 843
  • 2
  • 16
  • 36
  • Possible duplicate of [javascript excellentexport blob issue](http://stackoverflow.com/questions/40857752/javascript-excellentexport-blob-issue) – Endless Dec 04 '16 at 22:29
  • Just 1 day after you post again same kind of similar question... stick to one – Endless Dec 04 '16 at 22:29

0 Answers0