how to export into Microsoft excel from the coulmns of a table

Raj Kumar 0 Reputation points
2024-09-25T07:54:59.5133333+00:00

WhatsApp Image 2024-09-24 at 3.34.05 PM.jpeg

want to export data from the table as attached file into excel sheet in the attached format

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,823 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,474 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,251 Reputation points Microsoft Vendor
    2024-09-25T08:31:49.83+00:00

    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>
    

    User's image

    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

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.