如何使用javascript导出表内的div

时间:2017-02-27 08:34:45

标签: javascript html

我想将表格行中的特定div导出为ex​​cel。这该怎么做。如何在表ID中获取可添加的类div。

前:

<div class="addtable">
<table id="rer3233223"><tr><td>asasdasasd</td></tr></table>
<table id="rer32343433"><tr><td>asasdasasd</td></tr></table>
<table id="pijjj3232"><tr><td>asasdasasd</td></tr></table>
<table id="ldffddf32"><tr><td>asasdasasd</td></tr></table>
</div>

以下代码我用于根据id导出表。

 function fnExcelReport()
    {


        var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
        var textRange; var j=0;
        tab = document.getElementById('addtable'); // 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,"MileReport");
        }  
        else                 //other browser not tested on IE 11
            sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

        return (sa);
    }

1 个答案:

答案 0 :(得分:0)

<script>
    function fnExcelReport()
    {
        var elms = document.getElementById('addtable').getElementsByTagName('table');   

        var tab_text = new Array(); 

         for (var i = 0; i < elms.length; i++) {    

        tab_text[i]="<table border='2px'><tr bgcolor='#87AFC6'>";
        var textRange; var j=0;
        var jsids=elms[i].id;

        tab = document.getElementById(jsids); // id of table

        for(j = 0 ; j < tab.rows.length ; j++) 
        {     
            tab_text[i]=tab_text[i]+tab.rows[j].innerHTML+"</tr>";
            //tab_text=tab_text+"</tr>";
        }

        tab_text[i]=tab_text[i]+"</table>";

        tab_text[i]= tab_text[i].replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
        tab_text[i]= tab_text[i].replace(/<img[^>]*>/gi,""); // remove if u want images in your table
        tab_text[i]= tab_text[i].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,"MileReport");
        }  
        else                 //other browser not tested on IE 11
            sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

        return (sa);
    }
    </script>