HTML表导出到Excel(XLS或CSV) [英] HTML table export to Excel (XLS or CSV)

查看:146
本文介绍了HTML表导出到Excel(XLS或CSV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将HTML表格内容导出为ex​​cel。我看到此解决方案有效但不符合我的预期(因为 我无法选择哪个要复制的列 不适用于大表 )。


另一种解决方案是通过js复制并手动粘贴到excel文件,这不能正常工作,我真的不喜欢这种方法。

I'm trying to export HTML table content to excel. 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.

我想要的很简单,导出表的自定义视图,而不是所有列。向我展示一个我的意思的例子:



这是正常的表格视图:

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:

以下是我想在excel中展示的内容:

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

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

I would like a client side, cross-browser, workaround/solution, considering that I have around 2,500 lines in the table.

推荐答案

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>

这篇关于HTML表导出到Excel(XLS或CSV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆