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

3
我正在尝试将HTML表格内容导出到Excel。我看了这个解决方案,虽然它可以工作但不如我预期(因为我无法选择要复制的列,而且它不能用于大型表格)。

另外一种通过js复制并手动粘贴到Excel文件的解决方案也不起作用,我也不太喜欢这种方法。
简而言之,我想导出自定义视图的表格,而不是所有列。以下是示例,以说明我的意思:

这是正常的表格视图:

enter image description here

这是我想在Excel中显示的内容:

enter image description here

但由于我有隐藏字段,第一种方法不起作用:

enter image description here

我希望能够找到一个客户端、跨浏览器的解决方案来解决这个问题,考虑到表格中有大约2,500行。


1
我认为最好在服务器端构建Excel表格,这样您可以适当地进行自定义。 - Mohamed Farrag
你会使用任何框架,如Struts、Spring等吗? - Kanhu Bhol
@KanhuBhol,实际上,我被经典的ASP困住了! - user2517028
@MohamedFarrag,那将是我的最后手段! - user2517028
1
如果你改变了主意,我建议使用Office Open https://excelpackage.codeplex.com/ 这是一个非常好的工具,可以从头开始构建Excel表格。 - Mohamed Farrag
显示剩余4条评论
2个回答

1
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>

1

我做了一个非常类似的东西,并且每天在办公室使用,是在网上找到的教程。您可以使用this template来进行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标签生成简单的表格,并确保在头部编写正确的代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接