JavaScript Tens of thousands of data export Excel files (modified version)

Source: Internet
Author: User
Tags base64

The recent project to JS implementation of the data exported to Excel files, online many plug-ins to realize ~ ~ that happy, who know that the amount of data reached tens of thousands of problems: the browser not only stuck, the exported Excel file has been prompted network failure ....

Debug Debug found var Excel splicing table string, beyond the length limit of Var; Fortunately, there is a solution for seniors on the Web ~ ~ ~ Worship ing

Reference URL:51197552

Need to understand the JavaScript blob object yourself

/** * Export Excel File//example: var data = [{userName: ' Lily ', age:28, job: "Teacher", City: "Guangzhou"},{userName: ' Lucy ', age:19, job : "Doctor", City: "Shenzhen"}];if (data = = ") Return;var title = [{userName: ' name '}, {job: ' work '}, {age: ' old '} ‘ }]; Toexcel ("DataExport", data, title); ***//***toexcel: * FileName File name * jsondata exported data * ShowLabel Header*/varDOWNLOADEVT =NULL; functionToexcel (FileName, Jsondata, ShowLabel) {varArrdata =typeofJsondata! = ' object '?Json.parse (jsondata): Jsondata; var docData = new StringBuffer (); //Header    varInnerHTML =NewStringBuffer (); Docdata.append (' <table><thead> ');  for(vari = 0; i < showlabel.length; i++) {       for(varKeyinchShowlabel[i]) {Innerhtml.append (' <td> ' + showlabel[i][key] + ' </td>\r '); }} docdata.append (' <tr>\r ' + innerhtml.tostring () + ' </tr> '); Docdata.append (' </thead><tbody> '); //Row Vs Column     for(vari = 0; i < arrdata.length; i++) {      varTdrow =NewStringBuffer ();  for(vary = 0; Y < showlabel.length; y++) {             for(varKinchShowlabel[y]) {                if(Showlabel[y].hasownproperty (k)) {Tdrow.append (' <td style= ' vnd.ms-excel.numberformat:@ > ' + (arrdata[i][k] = = =NULL? ": arrdata[i][k]) + ' </td>\r '); }}} docdata.append (' <tr>\r ' + tdrow.tostring () + ' </tr> '); } docdata.append (' </tbody></table> '); Console.log ("DocData:", DocData); varDocfile =NewStringBuffer (); Docfile.append (' , docfile); Try{Console.log (' 111111111 ')        var blob = new Blob (docfile.content, {type: ' Application/vnd.ms-excel;charset=utf-8; ')        }); Console.log ("blob.size===" +blob.size); SaveAs (BLOB, FileName+ '. xlsx '); } Catch(e) {Console.log (' 22222222222 ') DownloadFile (FileName+ '. xlsx ',            ' Data:application/vnd.ms-excel;base64, ', docfile.tostring ()); }}function StringBuffer () {this.content = new Array;    } StringBuffer.prototype.append = function (str) {this.content.push (str);    } StringBuffer.prototype.prepend = function (str) {this.content.unshift (str);    } StringBuffer.prototype.toString = function () {return This.content.join (""); }  functionsaveAs (blob, filename) {varType =Blob.type; varForce_saveable_type = ' Application/octet-stream '; if(Type && type! = Force_saveable_type) {//force download, not open in browser        varSlice = Blob.slice | | Blob.webkitslice | |Blob.mozslice; Blob= Slice.call (blob, 0, Blob.size, Force_saveable_type); }    varURL =Url.createobjecturl (BLOB); varSave_link = Document.createelementns (' http://www.w3.org/1999/xhtml ', ' a '); Save_link.href=URL; Save_link.download=filename; Console.log (' Save_link: ', Save_link); varevent = document.createevent (' mouseevents '); Event.initmouseevent (' Click ',true,false, window, 0, 0, 0, 0, 0,false,false,false,false, 0,NULL);    Save_link.dispatchevent (event); Url.revokeobjecturl (URL);}functiondownloadFile (filename, header, data) {varUA =window.navigator.userAgent; if(Ua.indexof ("MSIE") > 0 | |!! Ua.match (/trident.*rv\:11\./)) {        //Internet Explorer (<= 9) Workaround by Darryl (Https://github.com/dawiong/tableExport.jquery.plugin)      //based on sampopes answer on http://stackoverflow.com/questions/22317951      // !  Not working for JSON and PDF format!       varframe = document.createelement ("iframe"); if(frame) {document.body.appendChild (frame); Frame.setattribute ("Style", "Display:none"); Frame.contentDocument.open ("Txt/html", "Replace");          Frame.contentDocument.write (data);          Frame.contentDocument.close ();          Frame.focus (); Frame.contentDocument.execCommand ("SaveAs",true, filename);        Document.body.removeChild (frame); }      }      Else {        varDownloadlink = document.createelement (' A '); if(downloadlink) {DownloadLink.style.display= ' None '; Downloadlink.download=filename; Console.log (Header+Base64Encode (data)). length); if(Header.tolowercase (). IndexOf ("base64,") >= 0) Downloadlink.href= Header +Base64Encode (data); ElseDownloadlink.href= encodeURIComponent (header +data);          Document.body.appendChild (Downloadlink); if(document.createevent) {if(Downloadevt = =NULL) downloadevt= Document.createevent (' mouseevents '); Downloadevt.initevent (' Click ',true,false);          Downloadlink.dispatchevent (DOWNLOADEVT); }          Else if(Document.createeventobject) downloadlink.fireevent (' OnClick '); Else if(typeofDownloadlink.onclick = = ' function ') Downloadlink.onclick (); /*Document.body.removeChild (downloadlink);*/        }      }    }  functionBase64Encode (input) {varKeystr = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; varOutput = ""; varchr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4; vari = 0; Input=Utf8encode (input);  while(I <input.length) {chr1= Input.charcodeat (i++); CHR2= Input.charcodeat (i++); CHR3= Input.charcodeat (i++); Enc1= Chr1 >> 2; ENC2= ((Chr1 & 3) << 4) | (CHR2 >> 4); Enc3= ((CHR2 &) << 2) | (CHR3 >> 6); Enc4= CHR3 & 63; if(IsNaN (CHR2)) {enc3= ENC4 = 64; } Else if(IsNaN (CHR3)) {Enc4= 64; } Output= output +Keystr.charat (ENC1)+ Keystr.charat (ENC2) +Keystr.charat (enc3)+Keystr.charat (ENC4); }      returnoutput; }    functionUtf8encode (String) {string= String.Replace (/\x0d\x0a/g, "\x0a"); varUtftext = "";  for(varn = 0; n < string.length; n++) {        varc =string.charcodeat (n); if(C < 128) {Utftext+=String.fromCharCode (c); }        Else if((C > 127) && (C < 2048) ) {Utftext+ = String.fromCharCode ((c >> 6) | 192); Utftext+ = String.fromCharCode ((C & 63) | 128); }        Else{Utftext+ = String.fromCharCode ((c >> 12) | 224); Utftext+ = String.fromCharCode (((c >> 6) & 63) | 128); Utftext+ = String.fromCharCode ((C & 63) | 128); }      }      returnUtftext; }  

JavaScript Tens of thousands of data export Excel files (modified version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.