2017-05-06 109 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工作表 –