Hi @Raj Kumar,
Is your project ASP.NET WebForm? Are you using HTML Table?
If yes, you can try the following code.
protected void ExportToExcel(object sender, EventArgs e)
{
Response.ContentType = "application/x-msexcel";
Response.AddHeader("Content-Disposition", "attachment;filename = ExcelFile.xls");
Response.ContentEncoding = Encoding.UTF8;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
tb.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
<form id="form1" runat="server">
<table cellspacing="0" runat="server" id="tb" cellpadding="2" style="border-collapse: collapse; border: 1px solid #ccc; font-size: 9pt;">
<tr>
<th style="background-color: #B8DBFD; border: 1px solid #ccc">Customer Id</th>
<th style="background-color: #B8DBFD; border: 1px solid #ccc">Name</th>
<th style="background-color: #B8DBFD; border: 1px solid #ccc">Country</th>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">1</td>
<td style="width: 150px; border: 1px solid #ccc">John Hammond</td>
<td style="width: 120px; border: 1px solid #ccc">United States</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">2</td>
<td style="width: 150px; border: 1px solid #ccc">Mudassar Khan</td>
<td style="width: 120px; border: 1px solid #ccc">India</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">3</td>
<td style="width: 150px; border: 1px solid #ccc">Suzanne Mathews</td>
<td style="width: 120px; border: 1px solid #ccc">France</td>
</tr>
<tr>
<td style="width: 120px; border: 1px solid #ccc">4</td>
<td style="width: 150px; border: 1px solid #ccc">Robert Schidner</td>
<td style="width: 120px; border: 1px solid #ccc">Russia</td>
</tr>
</table>
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="ExportToExcel" />
</form>
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread