js achieve large front-end export Excel files

//base64转换成blob
function dataURIToBlob(dataURI, callback) {
  var binStr = atob(dataURI.split(",")[1]),
    len = binStr.length,
    arr = new Uint8Array(len);

  for (var i = 0; i < len; i++) {
    arr[i] = binStr.charCodeAt(i);
  }

  callback(new Blob([arr]));
}

var callback = function(blob) {
  var a = document.createElement("a");
  a.download = "数据" + ".xls";
  a.innerHTML = "download";
  // the string representation of the object URL will be small enough to workaround the browser‘s limitations
  a.href = URL.createObjectURL(blob);
  // you must revoke the object URL,
  Operating since WE CAN But not // know the when occured at The download, WE have have The attach to the Click Handler at The IT ON .. 
  a.click (); 
}; 

// download export 
$ ( "down_box.") Click (function (e. ) { 
  $ .axs ( 
    Host + "/digital/communication/exportData.action", 
    {}, 
    function (Data) { 
      . $ (. "down_box") CSS ({ "pointer-Events": "", Opacity: " . 1 "}); 
      var = data.returnMap.exportData the exportData; 
      // the console.log (data.returnMap.exportData); 
      // column header 
      the let STR = 
        " <TR> <TD> time data </ td> <td> indicators number </ td> "+  
        " <td> index name </ td> <td> No. agency </ td> <td> name of </ td> <td> organization level </ td> "+
        " <td> parent organization number </ td> <td> name of the father </ td> <td> in real time value </ td><Td> real target </ td> <td> ratio in real time </ td> <td> real-time target value difference </ td> "+ 
        " <td> ratio in real time rankings </ td> <td> in real time than the red and green word </ td> <td> complete real-time warning value </ td > <td> last real-time </ TD> <td> last year real difference </ TD> "+ 
        " <TD> last year real </ TD> <td> Red-Green real word last year </ td> </ tr> "; 

      // loop through each row was added tr tag, each cell plus td tag 
      for (the let I = 0; I <exportData.length; I ++) { 
        STR + =" <tr> " ; 
        for (the let Item in the exportData [I]) { 
          // increase \ t to keep the display table in scientific notation, or other formats 
          str + = `<td> $ {exportData [i] [item] +" \ t "} </ td> `;
        }
        str += "</tr>";
      }
      //Worksheet名
      let worksheet = "Sheet1";
      let uri =
        "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,";
officedocument.spreadsheetml.sheet-openxmlformats; Base64, "; 
      // download the spreadsheet template data
      let 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><!--[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> STR $ {} </ Table> </ body> </ HTML> `; 
      // Download Template
      var template1 = uri + btoa(unescape(encodeURIComponent(template)));
      dataURIToBlob(template1, callback);
    },
    function() {
      $(".down_box").css({ "pointer-events": "", opacity: "1" });
    },
    function() {
      $(".down_box").css({ "pointer-events": "none", opacity: "0.5" });
    }
  );
});

  

HTML

<div class="down_box">下载<div/>

  

Note: as part of the package ajax ajax embodiment can modify the request 

Data Format:

 

Guess you like

Origin www.cnblogs.com/moli-/p/11540392.html