Skip to main content

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

#ThankYouCitrisys

MAY 5, 2015 an unforgettable day in my life. Why this award is so special to me? Because, after 8 years (After completion of Engineering), I landed on a good place where they identified me and involved me in the good projects with suitable PMOs. With all their support in Citrisys, I did small things with my involvement. For that, Citrisys, awarded me this "Award of Excellence". This award not shows me the best performance, but, it shows me to work more and more. Thank you Mr.ARA (CEO, Citrisys Solutions) for this award. #ThankYouCitrisys

Citrisys Poster Competition work 02