How to create an Excel file in UWP?
Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can start creating an Excel document in UWP.
Steps to create an Excel file programmatically:
- Create a new C# Blank App (Universal Windows) project.
- Install the Syncfusion.XlsIO.UWP NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Add a new button in the MainPage.xaml as shown below.
<Page x:Class="CreateXlsIOSample_UWP.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:CreateXlsIOSample_UWP" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Button x:Name="button" Content="Create Document" Click="OnButtonClicked" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Page>
- Include the following namespaces in the MainPage.xamal.cs file.
using Syncfusion.XlsIO; using Windows.Storage.Pickers; using Windows.Storage;
Imports Syncfusion.XlsIO Imports Windows.Storage.Pickers Imports Windows.Storage
- Include the below code snippet in the click event of the button in MainPage.xaml.cs, to create an Excel file and save the Excel document as a physical file and open the file for viewing.
// Create an instance of ExcelEngine.
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Set the default application version as Excel 2016.
excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016;
// Create a workbook with a worksheet.
IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
// Access the first worksheet from the workbook instance.
IWorksheet worksheet = workbook.Worksheets[0];
// Insert sample text into cell "A1".
worksheet.Range["A1"].Text = "Hello World";
// Save the Workbook.
StorageFile storageFile;
if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
savePicker.SuggestedFileName = "Sample";
savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });
storageFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
storageFile = await local.CreateFileAsync("Sample.xlsx", CreationCollisionOption.ReplaceExisting);
}
// Saving the workbook.
await workbook.SaveAsAsync(storageFile);
// Launch the saved file.
await Windows.System.Launcher.LaunchFileAsync(storageFile);
}
'Create an instance of ExcelEngine. Using excelEngine As ExcelEngine = New ExcelEngine 'Set the default application version as Excel 2016. excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016 'Create a workbook with a worksheet. Dim workbook As IWorkbook = excelEngine.Excel.Workbooks.Create(1) 'Access first worksheet from the workbook instance Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Insert sample text into cell A1. worksheet.Range("A1").Text = "Hello World" 'Save the Workbook Dim storageFile As StorageFile If Not Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons") Then Dim savePicker As FileSavePicker = New FileSavePicker savePicker.SuggestedStartLocation = PickerLocationId.Desktop savePicker.SuggestedFileName = "Sample" savePicker.FileTypeChoices.Add("Excel Files", New List(Of String)) storageFile = savePicker.PickSaveFileAsync Else Dim local As StorageFolder = Windows.Storage.ApplicationData.Current.LocalFolder storageFile = local.CreateFileAsync("Sample.xlsx", CreationCollisionOption.ReplaceExisting) End If 'Saving the workbook Function workbook_SaveAsAsync_((ByVal Unknown As storageFile) As await Implements workbook.SaveAsAsync.(Of End Function 'Launch the saved file Function Windows_System_Launcher_LaunchFileAsync_((ByVal Unknown As storageFile) As await Implements Windows.System.Launcher.LaunchFileAsync.(Of End Function End Using
A complete working example to create Excel file in UWP can be downloaded from Create-Excel-file.zip.
By executing the program, you will get the Excel file as follows.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like conditional formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through tables and pivot tables, appending multiple records to worksheet using template markers, and most importantly PDF and image conversions etc., with code examples.
Refer here to explore the rich set of Syncfusion® Essential XlsIO features.
An online sample link to generate an Excel file.
Conclusion:
I hope you enjoyed learning about how to create an Excel file in UWP.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!