Convert Excel document to Image in Azure App Service on Linux

7 Nov 20233 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 Image in Azure App Service on Linux.

Steps to convert Excel document to Image in Azure App Service on Linux

Step 1: Create a new ASP.NET Core Web Application (Model-View-Controller).

Create a ASP.NET Core Web App 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 as reference to your project from NuGet.org.

Install Syncfusion.XlsIORenderer.Net.Core NuGet Package
Install SkiaSharp NuGet Package
Install HarfBuzzSharp NuGet Package

NOTE

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: Add a new button in the Index.cshtml as shown below.

@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Create Document" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 6: Include the following namespaces in HomeController.cs.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Step 7: Include the below code snippet in HomeController.cs to convert an Excel document to Image.

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

  //Initialize XlsIO renderer.
  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;

  //Download image in the browser.
  return File(imageStream, "application/jpeg", "Sample.jpeg");
}

Steps to publish as Azure App Service on Linux

Step 1: Right-click the project and select Publish option.

Publish

Step 2: Select the publish target as Azure.

Add a Publish Profile

Step 3: Select the Specific target as Azure App Service (Linux).

Select the publish target

Step 4: To create a new app service, click Create new option.

Click create new option

Step 5: Click the Create button to proceed with App Service creation.

Hosting

Step 6: Click the Finish button to finalize the App Service creation.

App Service

Step 7: Click Close button.

Profile created

Step 8: Click the Publish button.

Start publish

Step 9: Now, Publish has been succeeded.

Publish has been succeeded

Step 10: Now, the published webpage will open in the browser.

Browser will open after publish

Step 11: Click Create Document to convert the given Excel document to Image. You will get the output Image as follows.

Output File

You can download a complete working sample from GitHub.

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.