HTML Table to Excel file - Export

The following code will convert the HTML Table to Excel file


<table id="headerTable">
<tr>
<th>Sl.No</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>Raja</td>
</tr>
<tr>
<td>2</td>
<td>Kannan</td>
</tr>
<tr>
<td>3</td>
<td>Harish</td>
</tr>
</table>

<button id="btnExport" onclick="tableToExcel('headerTable');"> EXPORT </button>
<iframe id="txtArea1" style="display:none"></iframe> <!-- THIS IFRAME IS REQUIRED -->
<script>
function tableToExcel(tblId)
{
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById(tblId); // 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,"Thanks");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

</script>



Source:
https://stackoverflow.com/questions/22317951/export-html-table-data-to-excel-using-javascript-jquery-is-not-working-properl

https://kayalshri.github.io/tableExport.jquery.plugin/

http://ngiriraj.com/pages/htmltable_export/demo.php#

Popular posts from this blog

[Solved] Hive installation error: java.net.URISyntaxException: Relative path in absolute URI

Install Android SDK in centOs

Installing arules package (Association Rules) in R