HTML表格导出到Excel(XLS或CSV)

我正试图将HTML表格内容导出为ex​​cel。 我看到这个解决scheme工作,但不是我所期望的(因为我不能select要复制的列 ,它不适用于大表 )。

另一种解决scheme是通过js复制并手动粘贴到excel文件中,这种方法并不奏效,而且我并不喜欢这种方法。

不久我想要的是,导出表格的自定义视图,而不是所有的列。 向你展示我的意思的例子:

这是普通的表格视图:

在这里输入图像说明

以下是我想要在Excel中显示的内容:

在这里输入图像说明

但是因为我有隐藏的领域,第一种方法不起作用:

在这里输入图像描述

我想要一个客户端,跨浏览器,解决scheme,考虑到我有大约2,500行在表中。

我做了一些非常相似的东西,每天在我的办公室使用它,并遵循networking上的教程。 您可以使用PHP的这个模板 :

<? $filename="sheet.xls"; header ("Content-Type: application/vnd.ms-excel"); header ("Content-Disposition: inline; filename=$filename"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang=it><head> <title>Titolo</title></head> <body> <table border="1"> <? for ($i=1;$i < 11; $i++) { echo "<tr>"; for ($j=1; $j<11;$j++) { $a = $i * $j; echo "<td>$a</td>"; } echo "</tr>"; } ?> </table> </body></html> 

你可以写一些类似于JS的东西,只需要生成一个带有HTML标签的简单表格,并确保在标题中写入正确的代码。

 Excel Export Script works on IE7+ , Firefox and Chrome =========================================================== function fnExcelReport() { var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>"; var textRange; var j=0; tab = document.getElementById('headerTable'); // id of table for(j = 0 ; j < tab.rows.length ; j++) { tab_text=tab_text+tab.rows[j].innerHTML+"</tr>"; //tab_text=tab_text+"</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, ""); // reomves 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,"Say Thanks to Sumit.xls"); } else //other browser not tested on IE 11 sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text)); return (sa); } Just Create a blank iframe <iframe id="txtArea1" style="display:none"></iframe> Call this function on <button id="btnExport" onclick="fnExcelReport();"> EXPORT </button>