Google Chrome + Javascript :Blob到Excel

我想保存一个<table>...</table> HTML标记作为一个Excel文件(所以它基本上是一个文件扩展名为.xls的表格html),我有一个代码,我从互联网(信用对所有者和感谢!)哪些适用于IE。

我现在的问题是,客户使用谷歌浏览器,因为它导出速度更快(文件尺寸更小),自部署数月后,下载的文件现在不被ms excel读取。 该文件被下载,但用MS-Excel打开时什么也没有出现甚至没有警告,文件已损坏或文件格式未知尝试打开? 信息。 有趣的是当用记事本++打开标记可以查看,删除一个空间,然后重新input然后保存>打开在Excel和中提琴现在可以查看..

我拥有的:

 if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer { txtArea1.document.open("txt/html","replace"); txtArea1.document.write($(tbl).html()); txtArea1.document.close(); txtArea1.focus(); sa=txtArea1.document.execCommand("SaveAs",true,"toExcel.xls"); return (sa); } else { try{ var blob = new Blob([$(tbl).html()]); var blobURL = window.URL.createObjectURL(blob); var a = document.createElement('a'); a.href = blobURL; a.download = 'toExcel.xls'; a.click(); } catch(err){ //console.log(err.message); alert(err.message); } } 

我试过了(仍不能被ms-excel读取):

 var blob = new Blob([$(tbl).html()],{type:'application/vnd.ms-excel'}); /*-----------------------------------------------------------------------*/ var blob = new Blob([$(tbl).html()],{type:"application/vnd.ms-excel"}); var reader = new window.FileReader(); reader.readAsDataURL(blob); reader.onloadend = function() { window.open(reader.result); } //this code works but if users export 1000++ rows of data it fails //URL max limit? /*---------------------------------------------------------------------*/ var blob = new Blob(['\ufeff', $(tbl).html()]); /*---------------------------------------------------------------------*/ var blob = new Blob(['<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" \n\ xmlns="http://www.w3.org/TR/REC-html40"><head>\n\ <!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>\n\ <x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->\n\ </head><body><table>'+$(tbl).html()+'</table></body></html>'],{type:"application/vnd.ms-excel"}); /*-----------------------------------------------------------*/ 

我也试过window.open('data:application/vnd.ms-excel'...)但是如果用户导出1000 ++行数据,这将无法正常工作…

即使听到并尝试FileSaver.js,但仍然没有解决的问题文件被下载,但不能打开的Excel,除非打开>编辑>保存np ++ …

另外我正在使用谷歌浏览器版本52.0.2743.116米…

最后的手段(将需要/必须检修代码…): Joel Coehoorn的答案

任何想法/build议谢谢!

https://support.microsoft.com/en-us/kb/3181507解释了这一&#x5207;…现在可以closures此线程…安装修补程序允许我查看下载的html.xls文件…