div内的Html表格导出为ex​​cel

<script type="text/javascript"> $(document).ready(function () { //getting values of current time for generating the file name $(".toExcelButton").click(function(){ var dt = new Date(); var day = dt.getDate(); var month = dt.getMonth() + 1; var year = dt.getFullYear(); var hour = dt.getHours(); var mins = dt.getMinutes(); var postfix = day + "." + month + "." + year + "_" + hour + "." + mins; //creating a temporary HTML link element (they support setting file names) var a = document.createElement('a'); //getting data from our div that contains the HTML table var data_type = 'data:application/vnd.ms-excel'; var table_div = document.getElementById('dvData'); var table_html = table_div.outerHTML.replace(/ /g, '%20'); a.href = data_type + ', ' + table_html; //setting the file name a.download = 'exported_table_' + postfix + '.xls'; //triggering the function a.click(); //just in case, prevent default behaviour e.preventDefault(); }) }); </script> 

需要将div表导出为ex​​cel。 上面的代码在Chrome中工作正常,但不能在IE中工作。 任何人都可以帮助我一样。

在IE中,一个dynamic创build的锚标签需要被添加到DOM来执行其点击事件。 此外,下载属性不支持在IE中:

下载属性在标签不工作在IE中

编辑:

最近我发布了很多处理这个问题的答案,这里有两个:

图像不会用它自己的扩展名下载

JS Base64string可下载pdf – 边缘

基本上你必须在IE中使用msSaveOrOpenBlob():

 var tF = 'Whatever.xls'; var tB = new Blob(..); if(window.top.navigator.msSaveOrOpenBlob){ //Store Blob in IE window.top.navigator.msSaveOrOpenBlob(tB, tF) } else{ //Store Blob in others var tA = document.body.appendChild(document.createElement('a')); tA.href = URL.createObjectURL(tB); tA.download = tF; tA.style.display = 'none'; tA.click(); tA.parentNode.removeChild(tA) } 

在上面的情况下:

 var tT = new XMLSerializer().serializeToString(document.querySelector('table')); //Serialised table var tF = 'Whatever.xls'; //Filename var tB = new Blob([tT]); //Blob if(window.top.navigator.msSaveOrOpenBlob){ //Store Blob in IE window.top.navigator.msSaveOrOpenBlob(tB, tF) } else{ //Store Blob in others var tA = document.body.appendChild(document.createElement('a')); tA.href = URL.createObjectURL(tB); tA.download = tF; tA.style.display = 'none'; tA.click(); tA.parentNode.removeChild(tA) } 

https://jsfiddle.net/23ao1v0s/1/

请检查下面给出的链接。 我想你会得到一个解决你的问题https://github.com/rainabba/jquery-table2excel