private async void Button_Click(object sender, RoutedEventArgs e)
{
SfDataGrid grid = new SfDataGrid();
GridQueryableCollectionViewWrapper wrapper = new GridQueryableCollectionViewWrapper((this.DataContext as OrderInfoViewModel).OrdersListDetails, grid);
grid.ItemsSource = wrapper;
grid.Columns.Add(new GridTextColumn() { MappingName = "CustomerID" });
grid.Columns.Add(new GridTextColumn() { MappingName = "Quantity" });
grid.ApplyTemplate();
var options = new ExcelExportingOptions();
options.ExcelVersion = ExcelVersion.Excel2013;
var excelEngine = grid.ExportToExcel(wrapper, options);
var workBook = excelEngine.Excel.Workbooks[0];
var savePicker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.Desktop,
SuggestedFileName = "Sample"
};
if (workBook.Version == ExcelVersion.Excel97to2003)
{
savePicker.FileTypeChoices.Add("Excel File (.xls)", new List<string>() { ".xls" });
}
else
{
savePicker.FileTypeChoices.Add("Excel File (.xlsx)", new List<string>() { ".xlsx" });
}
var storageFile = await savePicker.PickSaveFileAsync();
if (storageFile != null)
{
await workBook.SaveAsAsync(storageFile);
var msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully.");
var yesCmd = new UICommand("Yes");
var noCmd = new UICommand("No");
msgDialog.Commands.Add(yesCmd);
msgDialog.Commands.Add(noCmd);
var cmd = await msgDialog.ShowAsync();
if (cmd == yesCmd)
{
// Launch the saved file
bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile);
}
}
excelEngine.Dispose();
} |