How to export Excel file to browser from your ASP.NET applications?

Following example shows how to export Excel file to browser by using only GemBox.Spreadsheet .NET component.

GemBox.Spreadsheet is a C# / VB.NET component which provides simple and efficient way to read, write, convert and print native Microsoft Excel (XLSX, XLS) and other spreadsheet (ODS, CSV) formats and various document formats (HTML, PDF, XPS) from .NET applications without the need for Microsoft Excel on either the developer or client machines.
GemBox.Spreadsheet Free is free of charge while GemBox.Spreadsheet Professional is a commercial version licensed per developer.
For more information, see GemBox.Spreadsheet features and why GemBox.Spreadsheet outperforms Excel automation.

Example demonstrates Excel writing (DateTable export) to ASP.NET stream using GemBox.Spreadsheet.

C# code:

// Extract DataTable from current ASP.NET session.
var dataTable = (DataTable)this.Session["people"];

// Create Excel file.
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("DataSheet");

// Insert DataTable to worksheet.
worksheet.InsertDataTable(dataTable,
new InsertDataTableOptions("A1") { ColumnHeaders = true });

// Stream file to browser in requested file format.
workbook.Save(this.Response,
Path.ChangeExtension("Report", this.FileFormatRadioButtonList.SelectedValue));

Visual Basic .NET code:

' Extract DataTable from current ASP.NET session.
Dim dataTable = DirectCast(Me.Session("people"), DataTable)

' Create Excel file.
Dim workbook = New ExcelFile()
Dim worksheet = workbook.Worksheets.Add("DataSheet")

' Insert DataTable to worksheet.
worksheet.InsertDataTable(dataTable,
New InsertDataTableOptions("A1") With {.ColumnHeaders = True})

' Stream file to browser in requested file format.
workbook.Save(Me.Response,
Path.ChangeExtension("Report", Me.FileFormatRadioButtonList.SelectedValue))

For a more detailed example, see live ASP.NET sample from GemBox.Spreadsheet Sample Explorer.