Wednesday 5 June 2013

Export Html table data to Excel using jQuery

First of all add reference of jquery before apply this code.this code is usefull for export html table data in excel sheet.

write this in .aspx or html page

<div id="dvData">
<table>
    <tr>
        <th>Column One </th>
        <th>Column Two</th>
        <th>Column Three</th>
    </tr>
    <tr>
        <td>row1 Col1</td>
        <td>row1 Col2</td>
        <td>row1 Col3</td>
   </tr>
   <tr>
        <td>row2 Col1</td>
        <td>row2 Col2</td>
        <td>row2 Col3</td>
   </tr>
      <tr>
        <td>row3 Col1</td>
        <td>row3 Col2</td>
        <td>row3 Col3</td>  
   </tr>
</table>
</div>
<br/>
<input type="button" id="btnExport" value=" Export Table data into Excel " />

Add below code in script tag

$("#btnExport").click(function(e{
      window.open('data:application/vnd.ms-excel,' +encodeURIComponent($('#dvData').html()));
       e.preventDefault();
}); 

3 comments:

  1. when i m using this code in .aspx page. Every time empty popup window is opened.

    I m using IE8

    Please help..

    ReplyDelete
  2. IE not support so many scripts....
    use other browser like chrome,Firefox,safari,opera..etc

    ReplyDelete
  3. I am not able to change the excel sheet file name here..can you help me with that?

    ReplyDelete