Google Chrome + Javascript[jquery]: Blob to Excel

Google Chrome + Javascript[jquery]: Blob to Excel

本文关键字:Blob to Excel jquery Chrome Javascript Google      更新时间:2023-09-26

我试图将<table>...</table> html标记保存为excel文件(因此它基本上是一个带有。xls扩展名的文件中的表html),我有一个代码,我从互联网(归功于所有者和感谢!)这适用于IE。

我现在的问题是,客户端使用谷歌浏览器,因为它的导出速度更快(和更低的文件大小),几个月后部署的文件下载现在不被ms excel读取。文件下载,但当与ms-excel打开没有出现,甚至没有警告,文件损坏或文件格式未知尝试打开?消息。有趣的是,当用notepad++打开时,可以查看标记,删除一个空格,然后重新输入,然后保存>在excel中打开,现在可以查看了…

What i have:

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++行数据,这将不起作用…

即使听到并尝试过filesver .js,但仍然无法解决问题文件被下载,但无法通过excel打开,除非打开>edit>save on np++…

我也使用谷歌Chrome版本52.0.2743.116 m…

最后的手段(将需要/必须彻底检查代码…):乔尔·库霍恩的回答

任何想法/建议,谢谢!

https://support.microsoft.com/en-us/kb/3181507解释了这一切…现在可以关闭这个线程了…安装补丁使我可以查看下载的html.xls文件…