You can save or load Excel-formatted files and specify various save or load options.
You can specify whether to save to a BIFF or OfficeXML formatted file. BIFF (Binary Interchange File Format) is well known as the default file format for Microsoft Office Excel 97-2003, and the default file extension is .XLS. Office Open XML is well known as the default file format for Microsoft Office Excel 2007, and the default file extension is .XLSX. You can use the ExcelSaveFlags enumeration in the SaveExcelAsync method to specify additional options such as custom row and column headers or data only. You can also specify a password when exporting to a file.
You can specify additional load options with the ExcelOpenFlags enumeration in the OpenExcelAsync method. You can also specify a password string when loading an Excel-formatted file.
You can use the ExcelError event and the ExcelWarning class to return error codes and get unsupported records.
Charts are not imported and formulas in tables may not be imported correctly when loading Excel-formatted files.
Using Code
This example opens and saves Excel files.
CS |
Copy Code |
private async void Button_Click_1(object sender, RoutedEventArgs e) { try { var filePicker = new Windows.Storage.Pickers.FileSavePicker(); filePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xls", ".xlsx" }); filePicker.SuggestedFileName = "New SpreadSheet File"; Windows.Storage.StorageFile storageFile = await filePicker.PickSaveFileAsync(); if (storageFile != null) { using (var stream = await storageFile.OpenStreamForWriteAsync()) { var fileName = storageFile.FileType.ToUpperInvariant(); var fileFormat = GrapeCity.Xaml.SpreadSheet.Data.ExcelFileFormat.XLS; if (fileName.EndsWith(".XLSX")) fileFormat = GrapeCity.Xaml.SpreadSheet.Data.ExcelFileFormat.XLSX; else fileFormat = GrapeCity.Xaml.SpreadSheet.Data.ExcelFileFormat.XLS; await this.gcSpreadSheet1.SaveExcelAsync(stream, fileFormat, GrapeCity.Xaml.SpreadSheet.Data.ExcelSaveFlags.SaveAsViewed); } } } catch (Exception ex) { Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error"); dialog.ShowAsync(); } }
private async void Button_Click_2(object sender, RoutedEventArgs e) { try { var filePicker = new Windows.Storage.Pickers.FileOpenPicker(); filePicker.FileTypeFilter.Add(".xls"); filePicker.FileTypeFilter.Add(".xlsx"); Windows.Storage.StorageFile storageFile = await filePicker.PickSingleFileAsync(); if (storageFile != null) { using (var stream = await storageFile.OpenStreamForReadAsync()) { await this.gcSpreadSheet1.OpenExcelAsync(stream, GrapeCity.Xaml.SpreadSheet.Data.ExcelOpenFlags.RowAndColumnHeaders); } } } catch (Exception ex) { Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Error"); dialog.ShowAsync(); } } |
VB |
Copy Code |
Private Async Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Try Dim filePicker As New Windows.Storage.Pickers.FileSavePicker() filePicker.FileTypeChoices.Add("Excel Files", New List(Of String)() From {".xls", ".xlsx"}) filePicker.SuggestedFileName = "New SpreadSheet File" Dim storageFile As Windows.Storage.StorageFile = Await filePicker.PickSaveFileAsync()
If storageFile IsNot Nothing Then Using stream = Await storageFile.OpenStreamForWriteAsync() Dim fileName = storageFile.FileType.ToUpperInvariant() Dim fileFormat = GrapeCity.Xaml.SpreadSheet.Data.ExcelFileFormat.XLS If fileName.EndsWith(".XLSX") Then fileFormat = GrapeCity.Xaml.SpreadSheet.Data.ExcelFileFormat.XLSX Else fileFormat = GrapeCity.Xaml.SpreadSheet.Data.ExcelFileFormat.XLS End If Me.gcSpreadSheet1.SaveExcelAsync(stream, fileFormat, GrapeCity.Xaml.SpreadSheet.Data.ExcelSaveFlags.SaveAsViewed) End Using End If Catch ex As Exception Dim dialog As Windows.UI.Popups.MessageDialog = New Windows.UI.Popups.MessageDialog(ex.Message, "Error") dialog.ShowAsync() End Try End Sub Private Async Sub Button_Click_2(sender As Object, e As RoutedEventArgs) Try Dim filePicker As New Windows.Storage.Pickers.FileOpenPicker() filePicker.FileTypeFilter.Add(".xls") filePicker.FileTypeFilter.Add(".xlsx") Dim storageFile As Windows.Storage.StorageFile = Await filePicker.PickSingleFileAsync()
If storageFile IsNot Nothing Then Using stream = Await storageFile.OpenStreamForReadAsync() Me.gcSpreadSheet1.OpenExcelAsync(stream, GrapeCity.Xaml.SpreadSheet.Data.ExcelOpenFlags.RowAndColumnHeaders) End Using End If Catch ex As Exception Dim dialog As Windows.UI.Popups.MessageDialog = New Windows.UI.Popups.MessageDialog(ex.Message, "Error") dialog.ShowAsync() End Try End Sub |
See Also