Articles in this section
Category / Section

Convert Excel worksheet to image in ASP.NET Core.

2 mins read

Syncfusion Essential XlsIO is a ASP.NET Core Excel library used to create, read, and edit Excel documents. Using this library, you can convert Excel worksheet to image in ASP.NET Core.

Steps to convert Excel worksheet to image  programmatically:

  1. Create a new C# ASP.NET Core Web Application project.

Create ASP.NET Core application

 

  1. Select Web Application pattern (Model-View-Controller) for the project.

Select MVC

 

  1. Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as reference to your .NET Standard application from NuGet.org.
  2. A default controller with named HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in the HomeController.cs file.

C#

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

 

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

CSHTML

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

 

  1. Add a new action method CreateDocument in HomeController.cs and include the below code snippet to convert an Excel file to image and download it.

C#

//Initialize ExcelEngine.
using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Initialize Application.
    IApplication application = excelEngine.Excel;
 
    //Set default version for application.
    application.DefaultVersion = ExcelVersion.Excel2013;
 
    string basePath = _hostingEnvironment.WebRootPath + @"\XlsIO\sample.xlsx";
 
    FileStream sampleFile = new FileStream(basePath, FileMode.Open);
 
    //Open a workbook.
    IWorkbook workbook = application.Workbooks.Open(sampleFile);
 
    IWorksheet worksheet = workbook.Worksheets[1];
    
    // Initialize XlsIORenderer
    application.XlsIORenderer = new XlsIORenderer();
 
    //Create a new memory stream to save the image
    Stream stream = new MemoryStream();
 
    //Convert worksheet to image and save it to stream.
    worksheet.ConvertToImage(worksheet.UsedRange, stream);
            
   stream.Position = 0;
            
   //Download the converted Image in the browser.
   FileStreamResult fileStreamResult = new FileStreamResult(stream, "image/PNG");
   fileStreamResult.FileDownloadName = "Output.png";
   return fileStreamResult; 
}

 

A complete working example of how to convert Excel worksheet to image 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 supported features like 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 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.

 

Conclusion

I hope you enjoyed learning about how to convert Excel worksheet to image in ASP.NET Core.

You can refer to our ASP.NET Core XIsIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core XIsIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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