Javascript 如何在excel工作表中单击按钮下载html表格(不使用php)
我想在单击按钮时以excel格式下载此html表。请不要使用php和所有。使用javscript/css。请给出完整的代码,并粘贴相同的js文件所需的链接Javascript 如何在excel工作表中单击按钮下载html表格(不使用php),javascript,jquery,html,css,excel,Javascript,Jquery,Html,Css,Excel,我想在单击按钮时以excel格式下载此html表。请不要使用php和所有。使用javscript/css。请给出完整的代码,并粘贴相同的js文件所需的链接 <table id="tb1"> <tr> <td>Product</td> <td>Price</td> <td>Available</td> <td>Count
<table id="tb1">
<tr>
<td>Product</td>
<td>Price</td>
<td>Available</td>
<td>Count</td>
</tr>
<tr>
<td>Bred</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>Butter</td>
<td>4 </td>
<td>5 </td>
<td >6 </td>
</tr>
有很多方法可以让你实现它,你只需要去探索和学习。以下是我的建议。请检查这些代码,并根据您的需要修改代码 此方法适用于IE7+、Firefox、Chrome和Safari。下面是javascript:
function ToExcelReport(){
var tab_text="<table border='2px'><tr bgcolor='#FFFFFF'>";
var textRange; var j=0;
tab = document.getElementById('tb1'); //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, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); //remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); //remove input params
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)){
//if Internet Explorer
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",true,"Sample.xls");
} else{
//other browsers
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
}
return (sa);
}
最后,我所知道的最好的方法是使用服务器端语言,因为在这种情况下,有时客户端语言是一种痛苦。如果你有时间思考和改变你的人生观。。。笑话用法:我们不是有点专横。这里有一个链接,可以从它开始。对于一个你在谷歌上搜索过但没有找到的问题,最好是寻求支持。不要要求端到端的解决方案如果你能控制服务器端的涂鸦并使用php,那就去寻找。我认为你应该去那些提供免费下载所需文件的博客或网站。我们不会仅仅因为您要求就将完整的代码粘贴到这里。我只是想要一个关于如何将html表格下载到excel格式的适当示例。我用谷歌搜索了一下,但代码对我不起作用。
<iframe id="sample" style="display:none"></iframe>
<button id="btnExport" onclick="ToExcelReport();"> EXPORT </button>
$(document).ready( function () {
$('#tb1').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sSwfPath": "/swf/copy_cvs_xls_pdf.swf"
}
} );
} );