GemBox Software - .NET Components for Reading/Writing Excel files and Compound Document files  
Search gemboxsoftware.com:    
    
 
 
 
 
  FAQ
  Bugs & Features
  Articles



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 .NET component which provides easy and high performance way to write, read or convert native Microsoft Excel files (XLS, CSV or XLSX) and HTML files without the need for Microsoft Excel on either the developer or client machines. GemBox.Spreadsheet Free comes free of charge while GemBox.Spreadsheet Professional is a commercial version (licensed per developer). Find more information about GemBox.Spreadsheet features or reasons why our component is better then Excel Automation.

Example demonstrates XLS/XLSX writing (DateTable export) to ASP.NET stream using GemBox.Spreadsheet.

C# 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();


Visual Basic .NET code:

Dim people As DataTable = DirectCast(Session("people"), DataTable)

' Create excel file.

Dim ef As New ExcelFile()

Dim ws As ExcelWorksheet = ef.Worksheets.Add("DataSheet")

ws.InsertDataTable(people, "A1", True)

 

Response.Clear()

 

' Stream file to browser, in required type.

Select Case Me.RadioButtonList1.SelectedValue

    Case "XLS"

        Response.ContentType = "application/vnd.ms-excel"

        Response.AddHeader("Content-Disposition", "attachment; filename="

            & "Report.xls")

        ef.SaveXls(Response.OutputStream)

        Exit Select

 

    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.

        Dim ms As New MemoryStream()

        ef.SaveXlsx(ms)

        ms.WriteTo(Response.OutputStream)

        Exit Select

End Select

 

Response.End()


See live web sample that demonstrates CSV/XLS/XLSX/ODS/HMTL writing (DataTable export) to ASP.NET stream using GemBox.Spreadsheet.


















© GemBox Software. All rights reserved.