Excel to Image Conversion in Linux using Excel Library

14 Aug 20266 minutes to read

Syncfusion® XlsIO is a .NET Core Excel library used to create, read, edit, and convert Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can convert an Excel document to an image on Linux.

Prerequisites

Install the following before proceeding:

  • .NET SDK 6.0 or later (verify with dotnet --version).
  • Supported Linux distributions: Ubuntu 20.04+, Debian 11+, CentOS 8+, RHEL 8+, or equivalent.

    Steps to convert an Excel document to Image on Linux

Step 1: Create a new C# .NET Core console application. Create .NET Core console project in Visual Studio

Step 2: Name the project.

Name the project

Step 3: Select the framework and click Create button.

Framework version

Step 4: Install the following NuGet packages in your application from NuGet.org by executing the following commands:

dotnet add package Syncfusion.XlsIORenderer.Net.Core -v 30.1.37 -s https://www.nuget.org/
dotnet add package SkiaSharp.NativeAssets.Linux -v 3.119.1 -s https://www.nuget.org/
dotnet add package HarfBuzzSharp.NativeAssets.Linux -v 8.3.1.1 -s https://www.nuget.org/

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your applications to use our components.

Step 5: Include the following Namespaces in the Program.cs file.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Step 6: Add the following code snippet in Program.cs file.

The worksheet.ConvertToImage(IRange, Stream) overload used below requires the XlsIORenderer to be assigned to application.XlsIORenderer (already shown in the code) and writes the image to the supplied MemoryStream in PNG format. The file is saved with a .jpeg extension for compatibility; if you need a true JPEG, convert the PNG bytes or use the IImageOrPrintOptions overload. Ensure that a valid Syncfusion license key is registered before running.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];

    //Initialize XlsIORenderer (required by ConvertToImage(IRange, Stream))
    application.XlsIORenderer = new XlsIORenderer();

    //Create the MemoryStream to save the image  
    MemoryStream imageStream = new MemoryStream();

    //Save the converted image to MemoryStream
    worksheet.ConvertToImage(worksheet.UsedRange, imageStream);
    imageStream.Position = 0;

    #region Save
    FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.jpeg"), FileMode.Create, FileAccess.Write);
    imageStream.CopyTo(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();   
}

Step 1: Create a new C# .NET Core console application. Create .NET Core console project in Visual Studio Code

Step 2: Name the project and create the project.

Name the project

Alternatively, create a .NET Core console application using the following command in the terminal (Ctrl+`).

dotnet new console -o ConvertExcelToImage
cd ConvertExcelToImage

Step 3: Install the following NuGet packages in your application from NuGet.org by executing the following commands:

dotnet add package Syncfusion.XlsIORenderer.Net.Core -v 30.1.37 -s https://www.nuget.org/
dotnet add package SkiaSharp.NativeAssets.Linux -v 3.119.1 -s https://www.nuget.org/
dotnet add package HarfBuzzSharp.NativeAssets.Linux -v 8.3.1.1 -s https://www.nuget.org/

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your applications to use our components.

Step 4: Include the following Namespaces in the Program.cs file.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using System.IO;

Step 5: Add the following code snippet in Program.cs file.

The worksheet.ConvertToImage(IRange, Stream) overload used below requires the XlsIORenderer to be assigned to application.XlsIORenderer (already shown in the code) and writes the image to the supplied MemoryStream in PNG format. The file is saved with a .jpeg extension for compatibility; if you need a true JPEG, convert the PNG bytes or use the IImageOrPrintOptions overload. Ensure that a valid Syncfusion license key is registered before running.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];

    //Initialize XlsIORenderer (required by ConvertToImage(IRange, Stream))
    application.XlsIORenderer = new XlsIORenderer();

    //Create the MemoryStream to save the image  
    MemoryStream imageStream = new MemoryStream();

    //Save the converted image to MemoryStream
    worksheet.ConvertToImage(worksheet.UsedRange, imageStream);
    imageStream.Position = 0;

    #region Save
    FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.jpeg"), FileMode.Create, FileAccess.Write);
    imageStream.CopyTo(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();   
}

You can download a complete working sample from this GitHub page.

By executing the program, you will get the Image as follows.

Output File

Click here to explore the rich set of Syncfusion® Excel library (XlsIO) features.

An online sample link to convert an Excel document to Image in ASP.NET Core.