Loading and saving workbook in ASP.NET MVC

24 Jul 20263 minutes to read

Prerequisites

  • Visual Studio 2017 or later with the ASP.NET and web development workload installed.
  • .NET Framework 4.6.1 or later.
  • Install the Syncfusion.XlsIO.AspNet.Mvc5 NuGet package (or Syncfusion.XlsIO.AspNet.Mvc4 for MVC4) in your ASP.NET MVC project.
  • Register your Syncfusion® license key in your project. Refer to the licensing overview for details.

Include the following namespace in your HomeController.cs (or HomeController.vb) file.

using Syncfusion.XlsIO;
Imports Syncfusion.XlsIO

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add the Syncfusion.Licensing assembly reference and include a license key in your projects. Please refer to this link to know about registering the Syncfusion® license key in your applications to use our components.

Opening an existing workbook

You can open an existing workbook by using the overloads of the Open methods of the IWorkbooks interface.

// Create a new instance of ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
    // Initialize IApplication
    IApplication application = excelEngine.Excel;

    // Open an existing workbook through the Open method of IWorkbooks
    IWorkbook workbook = application.Workbooks.Open(Server.MapPath("App_Data/Sample.xlsx"));
}
' Create a new instance of ExcelEngine
Using excelEngine As New ExcelEngine()

' Initialize IApplication
Dim application As IApplication = excelEngine.Excel

' Open an existing workbook through the Open method of IWorkbooks
Dim workbook As IWorkbook = application.Workbooks.Open(Server.MapPath("App_Data/Sample.xlsx"))
End Using

Saving an Excel workbook

You can save the created or manipulated workbook using the overloads of the SaveAs methods.

// Create a new instance of ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
    // Initialize IApplication
    IApplication application = excelEngine.Excel;

    // Open an existing workbook
    IWorkbook workbook = application.Workbooks.Open(Server.MapPath("App_Data/Sample.xlsx"));

    // To-Do: some manipulation

    // Set the version of the workbook
    workbook.Version = ExcelVersion.Xlsx;

    // Stream the workbook to the HTTP response in xlsx format
    workbook.SaveAs("Output.xlsx", HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open);
}
' Create a new instance of ExcelEngine
Using excelEngine As New ExcelEngine()

' Initialize IApplication
Dim application As IApplication = excelEngine.Excel

' Open an existing workbook through the Open method of IWorkbooks
Dim workbook As IWorkbook = application.Workbooks.Open(Server.MapPath("App_Data/Sample.xlsx"))

' To-Do: some manipulation

' Set the version of the workbook
workbook.Version = ExcelVersion.Xlsx

' Stream the workbook to the HTTP response in xlsx format
workbook.SaveAs("Output.xlsx", HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open)
End Using

See Also