简体   繁体   English

HTML表导出到Excel(XLS或CSV)

[英]HTML table export to Excel (XLS or CSV)

I'm trying to export HTML table content to excel. 我正在尝试将HTML表格内容导出为ex​​cel。 I saw this solution which worked but not as I expected it (because I can't choose which columns to copy , and it doesn't works with big tables ). 我看到这个解决方案有效但不像我预期的那样(因为我无法选择要复制的列 ,并且它不适用于大表 )。

And another solution to copy by js and manually paste to excel file, which didn't work as well, and I don't really fancy this method. 另一种解决方案是通过js复制并手动粘贴到excel文件,这样做效果不好,我真的不喜欢这种方法。

Shortly what I want is, export customized view of the table, not all columns. 不久我想要的是,导出表的自定义视图,而不是所有列。 to show you an example of what I mean: 向您展示我的意思的一个例子:

Here is the normal table view: 这是正常的表视图:

在此输入图像描述

and Here is how what I want to show in excel: 以下是我想在excel中展示的内容:

在此输入图像描述

But because I have hidden fields, the first method didn't work: 但因为我有隐藏的字段,第一种方法不起作用:

在此输入图像描述

I would like a client side, cross-browser, workaround/solution, considering that I have around 2,500 lines in the table. 我想要一个客户端,跨浏览器,解决方案/解决方案,考虑到我在表中有大约2,500行。

I made something very similar and use it every day in my office following a tutorial found on the web. 我做了一些非常相似的事情,每天在我的办公室使用它,按照网上的教程。 You can use this template for PHP: 您可以将此模板用于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>

You can write something similar with JS, just generate a simple table with HTML tags and be sure to write the correct code in the header. 您可以使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM