Hi,
for exporting DataGrid to Excel in
Excel ASP.NET application, I recommend you take a look at this
Excel C# component.
It doesn't require
Excel Automation and API is pretty simple and intuitive:
Code:
var people = (DataTable)dataGrid.DataSource;
// Create excel file.
var ef = new ExcelFile();
var ws = ef.Worksheets.Add("DataSheet");
ws.InsertDataTable(people, "A1", true);
Response.Clear();
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.
var ms = new MemoryStream();
ef.SaveXlsx(ms);
ms.WriteTo(Response.OutputStream);
Response.End();