在' .xlsx'中将JSON数据导出到Excel格式

时间:2018-03-11 15:38:42

标签: javascript jquery json excel

我已经使用此函数将我的JSON数据转换为带样式的Excel。 Excel文件将以' .xls'保存。格式。但我需要将它保存在' .xlsx'格式。



var tableToExcel = (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><!--[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) {
	  
	  var col = [];
      for (var i = 0; i < table.length; i++) {
          for (var key in table[i]) {
              if (col.indexOf(key) === -1) {
                  col.push(key);
              }
          }
      }

      // CREATE DYNAMIC TABLE.
      var tempTable = document.createElement("table");
      tempTable.setAttribute("id", "testtable");

      // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

      var tr = tempTable.insertRow(-1);                   // TABLE ROW.

      for (var i = 0; i < col.length; i++) {
          var th = document.createElement("th");      // TABLE HEADER.
          th.innerHTML = col[i];
          tr.appendChild(th);
      }

      // ADD JSON DATA TO THE TABLE AS ROWS.
      for (var i = 0; i < table.length; i++) {

          tr = tempTable.insertRow(-1);

          for (var j = 0; j < col.length; j++) {
              var tabCell = tr.insertCell(-1);
              tabCell.innerHTML = table[i][col[j]];
          }
      }
	  
	  
	  
	  
	  
	  
    if (!table.nodeType) table = tempTable;
    var ctx = {worksheet: name || 'Worksheet', table: tempTable.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
&#13;
&#13;
&#13;

任何人都可以帮我将这些数据保存在&#39; .xlsx&#39;格式??此功能需要进行任何更改???

0 个答案:

没有答案