Wednesday, March 14, 2012

Export data table to Excel

To Export Data table to excel

following code can be useful


SPContext context = SPContext.Current;
System.Web.HttpResponse currentResponse = this.Response;
foreach (DataColumn column in table.Columns)
{
currentResponse.Write(column.ColumnName + "\t");
}
currentResponse.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++) { currentResponse.Write(row[i].ToString().Replace(";", string.Empty) + "\t"); } currentResponse.Write(Environment.NewLine); } currentResponse.ContentType = "text/xls"; currentResponse.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".xls"); currentResponse.End(); currentResponse.Close();
HOPE THIS HELPS SOMEONE

No comments:

Post a Comment