How to Export Data to Excel?

Answer Posted / santoshsutar1983

protected void Button3_Click(object sender, EventArgs e)

{

DataTable dt = GetData();

string attachment = "attachment;
filename=Employee.xls";

Response.ClearContent();

Response.AddHeader("content-disposition",
attachment);

Response.ContentType = "application/vnd.ms-excel";

string tab = "";

foreach (DataColumn dc in dt.Columns)

{

Response.Write(tab + dc.ColumnName);

tab = "\t";

}

Response.Write("\n");



int i;

foreach (DataRow dr in dt.Rows)

{

tab = "";

for (i = 0; i < dt.Columns.Count; i++)

{

Response.Write(tab + dr[i].ToString());

tab = "\t";

}

Response.Write("\n");

}

Response.End();

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

i have a gird with columns all are coming from database,this will bind in item templete in gridview as textboxex.and i have button below named Update.i want to update all the records in the grid,but if user change the value of one textbox,what is the easy way 2 do this

2322


Code for Document Validation in XML.NET?

2148


How to get Dynamically Linked Comboboxes Set?

2205


ArrayList declaration in .net

2967


Common UI for Multiple web applications. Suppose there are 35 websites using same third party controls.These 3rd party controls are made together that all 35 websites can use these controls.If we put all 3rd party controls and use its dll in 35 websites,only class files will be accessable. But I want to use CSS,images also in all 35 websites. how I can design the N-tier solution for this project.

2430


Code for Creating a Form Using PlaceHolder Controls?

2540


Code for Presenting Parent/Child Data in a Data Grid Row?

2283


how to track links visited in google using iframes

2460


How to Bind Nested XML to a Repeater Control with Container.DataItem?

3385


What is the code of Password Recovery or Forget your password? Plz tell in c # language.

3746


how to create a search bar which access data from various websites and retrieves the data

2732


How to use Client-side Script to Focus Controls in ASP.NET?

2592


How we use ajax in asp.net through javaScript. Please givee me an example.

2716


Coding for Synchronizing Cache Access in ASP.NET?

3326


Coding for .NET Delegates?

2292