View Single Post
  #4 (permalink)  
Old March 4th, 2010, 04:51 AM
cikaPero cikaPero is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
Reply With Quote