void OnButtonClicked(object sender, EventArgs e) { ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; #region Initializing Workbook //A new workbook is created.[Equivalent to creating a new workbook in MS Excel] //The new workbook will have 1 worksheets IWorkbook workbook = application.Workbooks.Create(1); //The first worksheet object in the worksheets collection is accessed. IWorksheet sheet = workbook.Worksheets[0]; Assembly assembly = typeof(App).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Template.customers.xml"); // Import the XML contents to worksheet XlsIOExtensions exten = new XlsIOExtensions(); exten.ImportXML(fileStream, sheet, 1, 1, true); // Apply style for header IStyle headerStyle = sheet[1, 1, 1, sheet.UsedRange.LastColumn].CellStyle; headerStyle.Font.Bold = true; headerStyle.Font.Color = ExcelKnownColors.Brown; headerStyle.Font.Size = 10; // Resize columns sheet.Columns[0].ColumnWidth = 11; sheet.Columns[1].ColumnWidth = 30.5; sheet.Columns[2].ColumnWidth = 20; sheet.Columns[3].ColumnWidth = 25.6; sheet.Columns[6].ColumnWidth = 10.5; sheet.Columns[4].ColumnWidth = 40; sheet.Columns[5].ColumnWidth = 25.5; sheet.Columns[7].ColumnWidth = 9.6; sheet.Columns[8].ColumnWidth = 15; sheet.Columns[9].ColumnWidth = 15; #endregion #region Saving workbook and disposing objects workbook.Version = ExcelVersion.Excel2013; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose(); if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("ImportXML.xlsx", "application/msexcel", stream); else Xamarin.Forms.DependencyService.Get<ISave>().Save("ImportXML.xlsx", "application/msexcel", stream); #endregion }
private async void btnImportXml_Click(object sender, RoutedEventArgs e) { StorageFile storageFile; if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.Desktop; savePicker.SuggestedFileName = "ImportXMLSample"; if (rdBtn2003.IsChecked.Value) { savePicker.FileTypeChoices.Add("Excel Files", new List <string>() { ".xls" }); } else { savePicker.FileTypeChoices.Add("Excel Files", new List <string>() { ".xlsx", }); } storageFile = await savePicker.PickSaveFileAsync(); } else { StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; if (rdBtn2003.IsChecked.Value) { storageFile = await local.CreateFileAsync("ImportXMLSample.xls", CreationCollisionOption.ReplaceExisting); } else { storageFile = await local.CreateFileAsync("ImportXMLSample.xlsx", CreationCollisionOption.ReplaceExisting); } } if (storageFile == null) { return; } //New instance of XlsIO is created.[Equivalent to launching MS Excel with no workbooks open]. //The instantiation process consists of two steps. //Step 1 : Instantiate the spreadsheet creation engine. ExcelEngine excelEngine = new ExcelEngine(); //Step 2 : Instantiate the excel application object. IApplication application = excelEngine.Excel; if (rdBtn2003.IsChecked.Value) { application.DefaultVersion = ExcelVersion.Excel97to2003; } else { application.DefaultVersion = ExcelVersion.Excel2013; } //A new workbook is created.[Equivalent to creating a new workbook in MS Excel] //The new workbook will have 3 worksheets IWorkbook workbook = application.Workbooks.Create(1); //The first worksheet object in the worksheets collection is accessed. IWorksheet sheet = workbook.Worksheets[0]; Assembly assembly = typeof(ImportXML).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.XlsIO.XlsIO.Tutorials.Samples.Assets.Resources.Templates.customers.xml"); // Import the XML contents to worksheet XlsIOExtensions exten = new XlsIOExtensions(); exten.ImportXML(fileStream, sheet, 1, 1, true); // Apply style for header IStyle headerStyle = sheet[1, 1, 1, sheet.UsedRange.LastColumn].CellStyle; headerStyle.Font.Bold = true; headerStyle.Font.Color = ExcelKnownColors.Brown; headerStyle.Font.Size = 10; // Autofit columns sheet.Columns[0].ColumnWidth = 11; sheet.Columns[1].ColumnWidth = 30.5; sheet.Columns[2].ColumnWidth = 20; sheet.Columns[3].ColumnWidth = 25.6; sheet.Columns[6].ColumnWidth = 10.5; sheet.Columns[4].ColumnWidth = 40; sheet.Columns[5].ColumnWidth = 25.5; sheet.Columns[7].ColumnWidth = 9.6; sheet.Columns[8].ColumnWidth = 15; sheet.Columns[9].ColumnWidth = 15; await workbook.SaveAsAsync(storageFile); workbook.Close(); excelEngine.Dispose(); MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully."); UICommand yesCmd = new UICommand("Yes"); msgDialog.Commands.Add(yesCmd); UICommand noCmd = new UICommand("No"); msgDialog.Commands.Add(noCmd); IUICommand cmd = await msgDialog.ShowAsync(); if (cmd == yesCmd) { // Launch the saved file bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile); } }