Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I need to export the form data in labels and also the grid data to excel. It's easy to export the grid data to excel in asp.net but if I want to export the form data as well, I am not able to find such code.

Please help.

Thanks

Varun Sareen
Posted

1 solution

What do you mean by form data? What ever the data may be have it in datatable object. Then convert the datatable object to excel.

Use this code:

DataTable dataTable = new DataTable();
        string attachment = "attachment; filename=Employee.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        GridView gvEmployee = new GridView();
        gvEmployee.DataSource = dataTable;
        gvEmployee.DataBind();
        gvEmployee.RenderControl(htextw);
        Response.Write(stw.ToString());
        Response.End();
 
Share this answer
 
v2
Comments
Varun Sareen 7-Mar-11 4:37am    
I mean that we have other data also on the form in labels or text boxes etc. how to export them in excel.?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900