Loading and saving workbook in UWP
8 Dec 20232 minutes to read
Opening an existing workbook
You can open an existing workbook by using the overloads of Open methods of IWorkbooks interface.
//Creates a new instance for ExcelEngine
ExcelEngine excelEngine = new ExcelEngine();
//Initialize IApplication
IApplication application = excelEngine.Excel;
//Instantiates the File Picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".xlsx");
openPicker.FileTypeFilter.Add(".xls");
StorageFile file = await openPicker.PickSingleFileAsync();
//Opens 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.
//Creates a new instance for ExcelEngine
ExcelEngine excelEngine = new ExcelEngine();
//Initialize IApplication
IApplication application = excelEngine.Excel;
//Instantiates the File Picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".xlsx");
openPicker.FileTypeFilter.Add(".xls");
StorageFile file = await openPicker.PickSingleFileAsync();
//Opens the workbook
IWorkbook workbook = await application.Workbooks.OpenAsync(file, ExcelOpenType.Automatic);
//To-Do some manipulation
//To-Do some manipulation
//Set the version of the workbook
workbook.Version = ExcelVersion.Xlsx;
//Initializes FileSavePicker
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
savePicker.SuggestedFileName = "Output";
savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });
//Creates a storage file from FileSavePicker
StorageFile storageFile = await savePicker.PickSaveFileAsync();
//Saves changes to the specified storage file
await workbook.SaveAsAsync(storageFile);