下载后使用javascript或jQuery打开excel文件?

时间:2018-01-15 11:27:03

标签: javascript jquery excel

当我从我的Web应用程序导出excel文件时,我需要在下载后通过默认程序直接打开它。

HTML:

<a onclick="fnExcelExport();">Export As File</a>
<iframe id="txtArea1" style="display:none"></iframe>

的javascript:

function fnExcelExport() {
var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j = 0;
tab = document.getElementById('tablee'); // id of table
for (j = 0 ; j < tab.rows.length ; j++) {
    tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
}
tab_text = tab_text + "</table>";
tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");
tab_text = tab_text.replace(/<img[^>]*>/gi, ""); 
in your table
tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); 
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  
{
    txtArea1.document.open("txt/html", "replace");
    txtArea1.document.write(tab_text);
    txtArea1.document.close();
    txtArea1.focus();
    sa = txtArea1.document.execCommand("SaveAs", true, "Say Thanks to Sumit.xls");
}
else                 
    sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}

0 个答案:

没有答案