Wednesday, May 6, 2009

Rendering the underlying DataTable to Excel

protected void RenderDataTableToExcel_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();

}

No comments:

Post a Comment

 
Locations of visitors to this page