Loading and saving workbook in WPF
24 Jul 20263 minutes to read
Prerequisites
- Visual Studio 2017 or later with the .NET desktop development workload installed.
- .NET Framework 4.6.1 or later.
- Install the Syncfusion.XlsIO.Wpf NuGet package (or Syncfusion.XlsIO.Net.Core for .NET Core/5+/6+/8+ WPF apps) in your project.
- Register your Syncfusion® license key in your project. Refer to the licensing overview for details.
Include the following namespace in your MainWindow.xaml.cs (or MainWindow.xaml.vb) file.
using Syncfusion.XlsIO;Imports Syncfusion.XlsIONOTE
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.Licensingassembly 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("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("Sample.xlsx")
End UsingSaving 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("Sample.xlsx");
// To-Do: some manipulation
// Set the version of the workbook
workbook.Version = ExcelVersion.Xlsx;
// Save the workbook to disk in xlsx format
workbook.SaveAs("Output.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("Sample.xlsx")
' To-Do: some manipulation
' Set the version of the workbook
workbook.Version = ExcelVersion.Xlsx
' Save the workbook to disk in xlsx format
workbook.SaveAs("Output.xlsx")
End Using