Articles in this section
Category / Section

How to create an Excel file in UWP?

1 min read

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 Excel file programmatically:

 

  1. Create a new C# Blank App (Universal Windows) project.

 

Create Blank App (Universal Windows) application in Visual Studio

 

  1. Install the Syncfusion.XlsIO.UWP NuGet package as reference to your .NET Framework applications from NuGet.org.

 

Add XlsIO reference to a project

 

  1. 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>

 

  1. 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

 

  1. 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 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.

Create Excel File

 

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 Excel file.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied