Articles in this section

How to convert Excel worksheet to image in Xamarin.Forms?

Syncfusion Essential® XlsIO is a Xamarin Excel library used to create, read, and edit Excel documents. Using this library, you can start converting an Excel worksheet to an image in Xamarin.

 

Steps to convert an Excel worksheet to an image programmatically:

 

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

Create Xamarin application

 

  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.

 

Select Xamarin.Forms project template and platforms

 

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

 

  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.

 

i)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.

 

ii)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())
  {
    IApplication application = excelEngine.Excel;
    // Set the default application version as Xlsx.
    application.DefaultVersion = ExcelVersion.Xlsx;
 
    Assembly executingAssembly = typeof(MainPage).GetTypeInfo().Assembly;
    Stream inputStream = executingAssembly.GetManifestResourceStream("GettingStarted.sample.xlsx");
 
    IWorkbook workbook = application.Workbooks.Open(inputStream);
 
    // Access the first worksheet from the workbook instance.
    IWorksheet worksheet = workbook.Worksheets[1];
 
    // Initialize XlsIORenderer
    application.XlsIORenderer = new XlsIORenderer();
 
    // Create a new memory stream to save the image
    MemoryStream stream = new MemoryStream();
 
    // Convert worksheet to image and save it to stream.
    worksheet.ConvertToImage(worksheet.UsedRange, stream);
    workbook.Close();
 
    // Save the stream as a file in the device and invoke it for viewing
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("out.png", "image/png", 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.

 

A complete working example to convert worksheet to image in Xamarin can be downloaded from Worksheet-to-Image.zip.

 

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

 

Worksheet to Image conversion

Know more about Essential® XlsIO through the documentation, where you can find more information of Worksheet to Image conversion with respective code examples and prerequisite.

 

Refer here to explore the rich set of Syncfusion Essential® XlsIO features.

 

Note:

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

 

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