2017-05-06 93 views

回答

0

你可以試試下面的功能:

function ExportToExcel(fileName) 
{ 
    var isIE = (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0); 
    if (isIE) { 
     // IE > 10 
     if (typeof Blob != 'undefined') { 
      var fileData = new Blob([document.getElementById("datatable").innerHTML.replace(/=" "/gi, "")], {type: 'application/vnd.ms-excel,'}); 
      window.navigator.msSaveBlob(fileData, fileName + '.xls'); 
     } 
     // IE < 10 
     else { 
      myFrame.document.open("text/html", "replace"); 
      myFrame.document.write(document.getElementById("datatable").innerHTML.replace(/=" "/gi, "")); 
      myFrame.document.close(); 
      myFrame.focus(); 
      myFrame.document.execCommand('SaveAs', true, fileName + '.xls'); 
     } 

    } 
    // crome,mozilla 
    else { 
    var uri = 'data:application/vnd.ms-excel,' + document.getElementById("datatable").innerHTML.replace(/ /g, '%20'); 
     var link = document.createElement("a"); 
     link.href = uri; 
     link.style = "visibility:hidden"; 
     link.download = fileName + ".xls"; 
     document.body.appendChild(link); 
     link.click(); 
     document.body.removeChild(link); 
    } 
} 

爲了使此功能工作,在舊的IE瀏覽器,你需要有和IFRAME與名myFrame,這架被用於在這個功能。另外,還要確保通過div的ID來包裝你的表DIV裏面,在這個片段是數據表

+0

感謝,讓我試試這個。我也希望它是base64。你可以告訴我嗎? –

+0

你想在base64中使用什麼?數據或Excel工作表? –

+0

是的,在html上呈現數據後的Excel工作表 –