Loading and saving workbook in UWP
24 Jul 20264 minutes to read
Prerequisites
- Visual Studio 2017 or later with the Universal Windows Platform development workload installed.
- Install the Syncfusion.XlsIO.UWP NuGet package in your UWP project.
- Register your Syncfusion® license key in your project. Refer to the licensing overview for details.
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.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 Open methods of IWorkbooks interface.
// Create a new instance of ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Initialize IApplication
IApplication application = excelEngine.Excel;
// Instantiate the File Picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".xlsx");
openPicker.FileTypeFilter.Add(".xls");
// Associate the picker with the current window (required for UWP desktop/bridged apps)
IntPtr hwnd = WindowNative.GetWindowHandle(App.MainWindow);
InitializeWithWindow.Initialize(openPicker, hwnd);
StorageFile file = await openPicker.PickSingleFileAsync();
// Open the workbook
IWorkbook workbook = await application.Workbooks.OpenAsync(file, ExcelOpenType.Automatic);
}Saving an Excel workbook
You can also save the created or manipulated workbook using overloads of SaveAs methods.
// Create a new instance of ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Initialize IApplication
IApplication application = excelEngine.Excel;
// Instantiate the File Picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".xlsx");
openPicker.FileTypeFilter.Add(".xls");
// Associate the picker with the current window (required for UWP desktop/bridged apps)
IntPtr hwnd = WindowNative.GetWindowHandle(App.MainWindow);
InitializeWithWindow.Initialize(openPicker, hwnd);
StorageFile file = await openPicker.PickSingleFileAsync();
// Open the workbook
IWorkbook workbook = await application.Workbooks.OpenAsync(file, ExcelOpenType.Automatic);
// To-Do: some manipulation
// Set the version of the workbook
workbook.Version = ExcelVersion.Xlsx;
// Initialize the FileSavePicker
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
savePicker.SuggestedFileName = "Output";
savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });
// Associate the picker with the current window (required for UWP desktop/bridged apps)
InitializeWithWindow.Initialize(savePicker, hwnd);
// Create a storage file from the FileSavePicker
StorageFile storageFile = await savePicker.PickSaveFileAsync();
// Save changes to the specified storage file
await workbook.SaveAsAsync(storageFile);
}