Click or drag to resize
ExcelFileSave Method (Object, String)
Saves the spreadsheet to a System.Web.HttpResponse or System.Web.HttpResponseBase output stream or a System.Net.Http.HttpResponseMessage and sets appropriate HTTP response headers (Content-Type and Content-Disposition). Use this method in ASP.NET application to send the spreadsheet to a client browser. File format is resolved from fileName extension. See remarks why httpResponse parameter is of type Object.

Namespace: GemBox.Spreadsheet
Assembly: GemBox.Spreadsheet (in GemBox.Spreadsheet.dll) Version: 41.3.30.1000
Syntax
public void Save(
	Object httpResponse,
	string fileName
)

Parameters

httpResponse
Type: SystemObject
System.Web.HttpResponse object that can be retrieved through the Response property of the System.Web.HttpApplication, System.Web.HttpContext, System.Web.UI.Page or System.Web.UI.UserControl classes or System.Web.HttpResponseBase object or System.Net.Http.HttpResponseMessage object.
fileName
Type: SystemString
File name that will be presented in client browser when downloading a file.
Exceptions
ExceptionCondition
InvalidOperationExceptionIf file extension is not specified or not supported. If file extension is not recognized, use Save(Object, String, SaveOptions) method overload instead.
Remarks
Note Note
Supported only if updating the whole page with a postback and not if doing partial-page updates (when you use UpdatePanel controls to update selected regions of a page).

This method accepts Object rather than HttpResponse or HttpResponseBase or HttpResponseMessage because otherwise GemBox.Spreadsheet assembly would have a dependency on System.Web or System.Net.Http assemblies and could not be used in applications which target Client Profile .NET Framework.

HttpResponse can be retrieved through the Response property of the HttpApplication, HttpContext, Page or UserControl classes.

For supported file extensions, see remarks for Save(String) method.

Examples

Following examples show how to stream spreadsheet to a client's browser using various ASP.NET technologies.

ASP.NET Web Forms

Method is defined inside a class that derives from System.Web.UI.Page.

private void ExportSpreadsheet()
{
    // Create a new empty spreadsheet.
    var workbook = new ExcelFile();

    // Add spreadsheet content.
    workbook.Worksheets.Add("Sheet1").Cells[0, 0].Value = "Hello world!";

    HttpResponse response = this.Response;

    // Stream spreadsheet to browser in XLSX format.
    workbook.Save(response, "Spreadsheet.xlsx");

}

ASP.NET Web Forms

Method is defined inside a class that derives from System.Web.UI.Page.

private void ExportSpreadsheet()
{
    // Create a new empty spreadsheet.
    var workbook = new ExcelFile();

    // Add spreadsheet content.
    workbook.Worksheets.Add("Sheet1").Cells[0, 0].Value = "Hello world!";

    HttpResponseBase responseBase = new HttpResponseWrapper(this.Response);

    // Stream spreadsheet to browser in XLSX format.
    workbook.Save(responseBase, "Spreadsheet.xlsx");
}

ASP.NET MVC

Method is defined inside a class that derives from System.Web.Mvc.Controller.

public ActionResult ExportSpreadsheet()
{
    // Create a new empty spreadsheet.
    var workbook = new ExcelFile();

    // Add spreadsheet content.
    workbook.Worksheets.Add("Sheet1").Cells[0, 0].Value = "Hello world!";

    byte[] fileContents;

    var options = SaveOptions.XlsxDefault;

    // Save spreadsheet to XLSX format in byte array.
    using (var stream = new MemoryStream())
    {
        workbook.Save(stream, options);

        fileContents = stream.ToArray();
    }

    // Stream spreadsheet to browser in XLSX format.
    return File(fileContents, options.ContentType, "Spreadsheet.xlsx");
}

ASP.NET Web API

Method is defined inside a class that derives from System.Web.Http.ApiController.

public HttpResponseMessage ExportSpreadsheet()
{
    // Create a new empty spreadsheet.
    var workbook = new ExcelFile();

    // Add spreadsheet content.
    workbook.Worksheets.Add("Sheet1").Cells[0, 0].Value = "Hello world!";

    var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);

    // Save spreadsheet to response message content in XLSX format.
    workbook.Save(responseMessage, "Spreadsheet.xlsx");

    return responseMessage;
}
See Also