2017-12-08 1 views
0

자바 스크립트 또는 jquery를 사용하여 모든 브라우저에서 comatibale을 사용하여 html 테이블 데이터를 내보내고 싶습니다. 나는이 스크립트는 모질라 파이어 폭스에서 잘 작동 기록이 많이 들어 스크립트엑셀 내보내기가 많지 않은 경우 Google 크롬이 작동하지 않음

 tableToExcel: function (table, name, sheetName) { 
     var e = this, fullTemplate = "", i, link, a; 

     e.uri = "data:application/vnd.ms-excel;base64,"; 
     e.base64 = function (s) { 
      return window.btoa(unescape(encodeURIComponent(s))); 
     }; 
     e.format = function (s, c) { 
      return s.replace(/{(\w+)}/g, function (m, p) { 
       return c[p]; 
      }); 
     }; 

     sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName; 

     e.ctx = { 
      worksheet: name || "Worksheet", 
      table: table, 
      sheetName: sheetName, 
     }; 

     fullTemplate = e.template.head; 

     if ($.isArray(table)) { 
      for (i in table) { 
       fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail; 
      } 
     } 

     fullTemplate += e.template.mid; 

     if ($.isArray(table)) { 
      for (i in table) { 
       fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail; 
      } 
     } 

     fullTemplate += e.template.foot; 

     for (i in table) { 
      e.ctx["table" + i] = table[i]; 
     } 
     delete e.ctx.table; 

     if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer 
     { 
      if (typeof Blob !== "undefined") { 
       //use blobs if we can 
       fullTemplate = [fullTemplate]; 
       //convert to array 
       var blob1 = new Blob(fullTemplate, { type: "text/html" }); 
       window.navigator.msSaveBlob(blob1, getFileName(e.settings)); 
      } else { 
       //otherwise use the iframe and save 
       //requires a blank iframe on page called txtArea1 
       txtArea1.document.open("text/html", "replace"); 
       txtArea1.document.write(e.format(fullTemplate, e.ctx)); 
       txtArea1.document.close(); 
       txtArea1.focus(); 
       sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings)); 
      } 

     } else { 
      link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); 
      a = document.createElement("a"); 
      a.download = getFileName(e.settings); 
      a.href = link; 

      document.body.appendChild(a); 

      a.click(); 

      document.body.removeChild(a); 
     } 

     return true; 
    } 

이하로 사용하고,하지만 크롬 브라우저에서 동일한 스크립트를 테스트 할 때 .I이 네트워크 오류 내가 실수를 할 을 얻고 작동하지 않습니다. 당신이 나를 도울 수?

답변

0

엑셀 내보내기 중에 너무 많은 레코드가있는 경우 충돌의 원인이 크롬의 URL 한도를 초과합니다. 이것은 크롬의 버그입니다.

다음 코드에서 데이터는 blob URL로 변환되어 내보내집니다. 내 문제가 해결되었습니다. 나는 이것이 다른 사람들에게도 유용하길 바란다.

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; 
    } 

이제 다음과 같은 기능을 사용할 수 있습니다.

var blob = b64toBlob(e.base64(e.format(fullTemplate, e.ctx)), "application/vnd.ms-excel"); 
var blobUrl = URL.createObjectURL(blob); 
a = document.createElement("a"); 
a.download = getFileName(e.settings); 
a.href = blobUrl; 
document.body.appendChild(a); 
a.click(); 
document.body.removeChild(a); 
관련 문제