HelpBot Assistant

How can I help you?

Convert Excel document to Image in ASP.NET Core

5 Nov 20256 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 ASP.NET Core.

Steps to convert Excel document to Image in ASP.NET Core

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

Create ASP.NET Core Web application in Visual Studio

Step 2: Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.XlsIORenderer.Net.Core 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 application to use our components.

Step 3: Include the following namespaces in the HomeController.cs file.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Step 5: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml.

Step 6: Add a new button in the Index.cshtml as shown below.

@{
    Html.BeginForm("ConvertExceltoImage", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Convert Excel to Image" style="width:200px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 7: Add a new action method ConvertExceltoImage in HomeController.cs and include the below code snippet to convert the Excel document to image.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
  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");
}

Step 1: Create a new ASP.NET Core Web application project.

  • Open the command palette by pressing Ctrl+Shift+P and type .NET:New Project and enter.
  • Choose the ASP.NET Core Web App( Model-View-Controller) MVC template.

Choose ASP.NET Core Web app from template

  • Select the project location, type the project name and press enter.
  • Then choose Create project.

Step 2: To convert a Excel document to image in ASP.NET Core Web app, install Syncfusion.XlsIORenderer.Net.Core to the ASP.NET Core project.

  • Press Ctrl + ` (backtick) to open the integrated terminal in Visual Studio Code.
  • Ensure you’re in the project root directory where your .csproj file is located.
  • Run the command dotnet add package Syncfusion.XlsIORenderer.Net.Core to install the NuGet package.

Add Syncfusion.XlsIORenderer.Net.Core 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 application to use our components.

Step 3: Include the following namespaces in the HomeController.cs file.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Step 4: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml.

Step 5: Add a new button in the Index.cshtml as shown below.

@{
    Html.BeginForm("ConvertExceltoImage", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Convert Excel to Image" style="width:200px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 6: Add a new action method ConvertExceltoImage in HomeController.cs and include the below code snippet to convert the Excel document to image.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
  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");
}

You can download a complete working sample from GitHub.

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

Excel to Image in ASP.NET Core

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.