0

I have created a dynamic table and I am binding data set values to this dynamic table. I want this dynamic table to export to Excel . Here is the code

 string table = string.Empty;
 table += "<table cellpadding='0' cellspacing='0' width='100%' 
style='border-left:solid 1px #bdbdbd;background-color:#ffffff;height:30px;'>";
table += "<tr><td colspan='2' class='cont' style='width: 50%; font-weight: bold; 
padding-left: 10px; border-right:solid 1px #bdbdbd; border-bottom:solid 1px 
#bdbdbd;background-color:#ffffff;height:30px; '>Mentor Name :&nbsp;" + 
dss.Tables[0].Rows[j]["FirstName"].ToString() + "</td></tr></table>";
dv1.InnerHtml = table;

Here dv1 is the id of the div which i am concatenating table to div

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
user1826608
  • 1
  • 1
  • 3
  • Refer below links may help you http://stackoverflow.com/questions/2974280/html-to-excel-simple-question http://stackoverflow.com/questions/2232211/exporting-html-to-excel-without-losing-formatting – Murali Murugesan Nov 16 '12 at 10:01
  • please also specify a question – K_B Nov 16 '12 at 10:02

1 Answers1

0

Please put your table in div and you can use the jquery code as below.

// Html code

<br/>
<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 " />

// Jquery Code

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

You can also use the below link for the view demo for this code.

http://jsfiddle.net/jquerybyexample/xhYcD/

Hope you can get your requirement from this code.

Mayur Desai
  • 683
  • 5
  • 13