Add the following code to save the Excel workbook. When you click the SaveButton, you will be able to save the project you created to any location.
- Edit the SaveButton_Click event to resemble the following code:
C# |
Copy Code
|
async void SaveButton_Click(object sender, RoutedEventArgs e)
{
}
|
- Insert the following code into the SaveButton_Click event to handle saving the Excel workbook:
C# |
Copy Code
|
Debug.Assert(_book != null);
var picker = new Windows.Storage.Pickers.FileSavePicker();
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
picker.FileTypeChoices.Add("Open XML Excel file", new List < string > ()
{
".xlsx"
});
picker.FileTypeChoices.Add("BIFF Excel file", new List < string > ()
{
".xls"
});
picker.SuggestedFileName = "New Book";
var file = await picker.PickSaveFileAsync();
if (file != null)
{
try
{
// step 1: save file
var fileFormat = Path.GetExtension(file.Path).Equals(".xls") ? FileFormat.OpenXmlTemplate : FileFormat.OpenXml;
await _book.SaveAsync(file, fileFormat);
// step 2: user feedback
_tbContent.Text = string.Format("File has been saved to: {0}.", file.Path);
RefreshView();
} catch (Exception x)
{
_tbContent.Text = string.Format("EXCEPTION: {0}", x.Message);
}
}
|
See Also