簡體   English   中英

在IE11中將HTML導出到Excel –“訪問被拒絕”

[英]Exporting HTML To Excel in IE11 – “Access is Denied”

我需要導出HTML報告以在各種不同的瀏覽器(最好僅在客戶端)中表現出色。 我已經花了很多時間解決這個問題,而且我有針對IE10 +和FF / Chrome的適用於瀏覽器的有效解決方案。 我正在尋找一種跨瀏覽器的解決方案,因為我不喜歡瀏覽器特定的代碼。

var saveData = (function () {
  var a = document.createElement("a");
  document.body.appendChild(a);
  a.style = "display: none";
  return function (data, fileName) {
    var html = data,
    blob = new Blob([html], { type: "text/xml" }),
    url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = fileName;
    a.click();
    window.URL.revokeObjectURL(url);
  };
}());

var fileName = "download.xls"
saveData($('#TablesContainer').html(), fileName);

http://codepen.io/mhodges44/pen/dGWZvN

此解決方案在FF / Chrome中有效,並且大多數情況下在IE中有效,但是在a.click()執行時出現以下錯誤:“ SCRIPT5:訪問被拒絕。”之前,我已經以編程方式在IE11中觸發了點擊事件,所以我不知道為什么拒絕執行。 它與文件下載的XSS安全性有關嗎?

請不要使用“ window.open('data:application…。')”解決方案或navigator.msSaveBlob()解決方案作為響應,因為它們不兼容跨瀏覽器。

最壞的情況是,我只剩下瀏覽器特定的代碼,但是希望有一個我沒有看到或想到的解決方案。

多謝你們!

if window.navigator and window.navigator.msSaveOrOpenBlob
      blob = new Blob([ data ], type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
      window.navigator.msSaveOrOpenBlob blob
else 
      blob = new Blob([ data ], type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
      objectUrl = URL.createObjectURL(blob)
      window.open objectUrl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM