Articles in this section
Category / Section

How to create an Excel file in Xamarin?

2 mins 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 Xamarin.

Steps to create Excel file programmatically:

 

  1. Create a new C# Xamarin.Forms application project.

 

Create Xamarin.Forms application in Visual Studio

 

  1. Select a project template and required platforms to deploy the application. In this application the portable assemblies to be shared across multiple platforms, the .NET Standard code sharing strategy has been selected. For more details about code sharing refer here.

 

Note: If .NET Standard is not available in the code sharing strategy, the Portable Class Library (PCL) can be selected.

 

Select Xamarin.Forms project template and platforms

 

  1. Install Syncfusion.Xamarin.XlsIO NuGet package as a reference to the .NET Standard project in your Xamarin applications from NuGet.org.

 

Add XlsIO reference to the project

 

  1. Add new Forms XAML page in portable project If there is no XAML page is defined in the App class. Otherwise proceed to the next step.

 

  1. To add the new XAML page, right click on the project and select Add > New Item and add a Forms XAML Page from the list. Name it as MainXamlPage.

 

  1. In App class of portable project (App.cs), replace the existing constructor of App class with the code snippet given below which invokes the MainXamlPage.

 

public App()
{
    // The root page of your application
    MainPage = new MainXamlPage();
 
}

 

  1. In the MainXamlPage.xaml add new button as shown below.

 

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GettingStarted. MainXamlPage">
<StackLayout VerticalOptions="Center">
 
<Button Text="Generate Document" Clicked="OnButtonClicked" HorizontalOptions="Center"/>
 
</StackLayout> </ContentPage>

 

  1. Include the following namespace in the MainXamlPage.xaml.cs file.
using Syncfusion.XlsIO;

 

  1. Include the below code snippet in the click event of the button in MainXamlPage.xaml.cs, to create an Excel file and save it in a stream.

 

void OnButtonClicked(object sender, EventArgs args)
{
  //Create an instance of ExcelEngine.
  using (ExcelEngine excelEngine = new ExcelEngine())
  {
    //Set the default application version as Excel 2013.
    excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2013;
 
    //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];
 
    //Adding text to a cell
    worksheet.Range["A1"].Text = "Hello World";
 
    //Save the workbook to stream in xlsx format. 
    MemoryStream stream = new MemoryStream();
    workbook.SaveAs(stream);
 
    workbook.Close();
 
    //Save the stream as a file in the device and invoke it for viewing
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("GettingStared.xlsx", "application/msexcel", stream);
  }
}

 

  1. Download the helper files from this link and add them into the mentioned project. These helper files allow you to save the stream as a physical file and open the file for viewing.

 

Project

File Name

Summary

Protable project

ISave.cs

Represent the base interface for save operation

iOS Project

SaveIOS.cs

Save implementation for iOS device

PreviewControllerDS.cs

Helper class for viewing the Excel file in iOS device

Android project

SaveAndroid.cs

Save implementation for Android device

WinPhone project

SaveWinPhone.cs

Save implementation for Windows phone device

UWP project

SaveWindows.cs

Save implementation for UWP device.

Windows(8.1) project

SaveWindows81.cs

Save implementation for WinRT device.

 

  1. Compile and execute the application. Now this application creates a simple Excel document.

 

By executing the program, you will get the Excel file as follows.

Create Excel File

 

A complete working example of how to create Excel File in Xamarin can be downloaded from Create-Excel-file.zip.

 

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.

 

See Also:

 

Create a Excel file in ASP.NET MVC

Create a Excel file in ASP.NET Core

Create a Excel file in Windows Forms

Create a Excel file in WPF

 

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 (1)
Please  to leave a comment
BS
Bhabani Sahoo

Hi All, I am new to Xamarin and stuck with something. Please get me an idea how to solve this...

Details:

  1. Create and Save a Excel file (I have successfully created one by using ISave and Xamarin.Forms.DependencyService.Get().SaveAndView("GettingStared.xlsx", "application/msexcel", stream);
  2. I dont get any error message and hence I assume that it has been created in my root directory
  3. Then I am trying to either attach that file as an email attachment (UNABLE TO DO SO ???)
  4. I have an exercise of converting my data in a ObservableCollection into a CSV file and attached that file into email and successfully sent an email after attaching the same csv file. I have also verified my mailbox and the csv file has been attached successfully in my email mailbox
  5. Now, I am trying the same with excel file. I have used XLSIO library of SyncFusion and done the coding but can not attach the excel file

My main objective: I have a standalone application which allows user to upload an excel file. Once a file is choosen by user, the application successfully reads the data and shows in a form. Then user performs editing the data and after all edit is done, then I want the user to save the data back in somewhere. The only way I did was to send an email by converting my latest data into a csv file and attach that file in email and allow user to send that email. I have attached my gmail account to do so and everything works. But I want to extend my solution by playing with excel. I can successfully read the excel file and load data from 3 sheets. Then user edits the data and my final data is in my observable collection. Then while creating a excel file dynamically and attaching as an attachment, I FAILED. I just want to save the data back to my storage somehow or send a copy in an email attachment.

Any help will be much appreciated pleaseeee. Kind regards

SK
Shamini Kiruba Sobers

Hi Bhabani,

Greetings from Syncfusion.

XlsIO doesn’t have support to attach files as an email attachment. Kindly try Xamarin.Essentials to do so. https://docs.microsoft.com/en-us/xamarin/essentials/email?tabs=android#file-attachments

You can save the edited Excel file to your storage successfully with the help XlsIO, which is shown in the following UG link. The saved Excel file can be sent as an attachment in a mail using Xamarin.Essentials.

https://help.syncfusion.com/file-formats/xlsio/loading-and-saving-workbook?cs-save-lang=1&cs-lang=xamarin#saving-a-excel-workbook-to-file-system

Please let us know if this helps.

Regards, Shamini

Access denied
Access denied