Hi,
you can easily export DataGrid to Excel in your
Excel ASP.NET application with this
Excel C# component.
Here is a sample code (to change what columns to export just use another overload of InsertDataTable method):
Code:
DataTable people = (DataTable)Session["people"];
// Create excel file.
ExcelFile ef = new ExcelFile();
ExcelWorksheet ws = ef.Worksheets.Add("DataSheet");
ws.InsertDataTable(people, "A1", true);
Response.Clear();
// Stream file to browser, in required type.
switch (this.RadioButtonList1.SelectedValue)
{
case "XLS":
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
"Report.xls");
ef.SaveXls(Response.OutputStream);
break;
case "XLSX":
Response.ContentType = "application/vnd.openxmlformats";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
"Report.xlsx");
// With XLSX it is a bit more complicated as MS Packaging API
// can't write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
MemoryStream ms = new MemoryStream();
ef.SaveXlsx(ms)
ms.WriteTo(Response.OutputStream);
break;
}
Response.End();