【问题标题】:HTML table export to Excel (XLS or CSV)HTML 表格导出到 Excel(XLS 或 CSV)
【发布时间】:2014-03-04 11:32:10
【问题描述】:

我正在尝试将 HTML 表格内容导出到 Excel。我看到 this solution 有效,但没有达到我的预期(因为我无法选择要复制的列,而且它没有适用于大桌子)。

还有另一种通过js复制并手动粘贴到excel文件的解决方案,效果也不好,我也不是很喜欢这种方法。

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

这是正常的表格视图:

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

但是因为我有隐藏字段,所以第一种方法不起作用:

考虑到我的表格中有大约 2,500 行,我想要一个客户端、跨浏览器、解决方法/解决方案。

【问题讨论】:

  • 我觉得如果你在服务器端构建excel表格会更好,这样你就可以适当地自定义它。
  • 你会使用struts、spring等框架吗
  • @KanhuBhol 实际上,我被古典 ASP 困住了!!
  • @MohamedFarrag,这将是我最后的选择!
  • 如果你改变了主意,所以我推荐使用 Office Open excelpackage.codeplex.com 它是一个从头开始构建 Excel 工作表的好工具。

标签: javascript html excel xlsx


【解决方案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标签的简单表格,并确保在表头中编写正确的代码。

【讨论】:

    【解决方案2】:
    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多