我正在尝试将一个<table>...</table> html标记保存为excel文件(所以它基本上是一个带有.xls扩展名的文件中的一个表格html ),我有一个我从互联网上得到的代码(归功于所有者,谢谢!)它适用于IE。
我现在的问题是,客户使用google chrome,因为它的导出速度更快(文件更小),在部署后的几个月里,下载的文件现在不能被ms excel读取。文件已下载,但当使用ms-excel打开时,什么也没有出现,甚至没有文件已损坏或文件格式未知的警告。尝试打开?消息。有趣的是,当使用notepad++打开标记时,可以查看它,删除一个空格,然后重新输入它,然后在excel和viola中使用save>open它现在可以查看。
我所拥有的:
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打开,除非open>edit>save on np++...
另外,我正在使用谷歌浏览器版本52.0.2743.116m...
最后一招(需要/必须彻底修改代码...):Joel Coehoorn's Answer
有任何想法/建议,谢谢!
发布于 2016-08-18 17:06:12
https://support.microsoft.com/en-us/kb/3181507解释了这一切...现在可以关闭此线程...安装修补程序后,我可以查看下载的html.xls文件...
https://stackoverflow.com/questions/38988978
复制相似问题