Convert Excel document to Image in Azure Functions v1

13 Jun 20245 minutes to read

Syncfusion XlsIO is a .NET 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 Functions v1.

Steps to convert an Excel document to Image in Azure Functions v1

Step 1: Create a new Azure Functions project.

Create an Azure Functions project in visual studio

Step 2: Name the project.

Name the project

Step 3: Select functions worker as .NET Framework v1.

Select functions worker

Step 4: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.XlsIO.WinForms 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: Include the following namespaces in the Function1.cs file.

using Syncfusion.XlsIO;

Step 6: Add the following code snippet in Run method of Function1 class to perform Excel to Image conversion in Azure Functions and return the resultant Image to client end.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;

  //Gets the input Excel document as stream from request.
  Stream excelStream = req.Content.ReadAsStreamAsync().Result;

  //Load the stream into IWorkbook.
  IWorkbook workbook = application.Workbooks.Open(excelStream);

  IWorksheet worksheet = workbook.Worksheets[0];

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

  //Save the image to MemoryStream.
  worksheet.ConvertToImage(worksheet.UsedRange.Row, worksheet.UsedRange.Column, worksheet.UsedRange.LastRow, worksheet.UsedRange.LastColumn, ImageType.Bitmap, imageStream);
  imageStream.Position = 0;

  //Create the response to return.
  HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

  //Set the image saved stream as content of response.
  response.Content = new ByteArrayContent(imageStream.ToArray());

  //Set the contentDisposition as attachment.
  response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  {
    FileName = "Sample.jpeg"
  };

  //Set the content type as image mime type.
  response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/jpeg");

  //Return the response with output image stream.
  return response;
}

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

Publish

Step 7: Select the publish target as Azure.

Add a Publish Profile"

Step 8: Select the Create new button.

Click create new option

Step 9: Click the Create button to proceed with creation.

Hosting

Step 10: Click the Finish button to finalize the Azure Function creation.

Creating app service

Step 11: Click Close button.

Profile created

Step 12: Click the Publish button.

Click Publish Button

Step 13: Publish has been succeeded.

Publish succeeded

Step 14: Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it in the below client sample (which will request the Azure Functions, to perform Excel to Image conversion using the template Excel document). You will get the output image as follows.

Output File

Steps to post the request to Azure Functions

Step 1: Create a console application to request the Azure Functions API.

Step 2: Add the following code snippet into Main method to post the request to Azure Functions with template Excel document and get the resultant image.

//Reads the template Excel document.
FileStream excelStream = new FileStream("../../../Sample.xlsx", FileMode.Open, FileAccess.Read);
excelStream.Position = 0;

//Saves the Excel document in memory stream.
MemoryStream inputStream = new MemoryStream();
excelStream.CopyTo(inputStream);
inputStream.Position = 0;

try
{
  Console.WriteLine("Please enter your Azure Functions URL :");
  string functionURL = Console.ReadLine();

  //Create HttpWebRequest with hosted azure functions URL.                
  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(functionURL);

  //Set request method as POST
  req.Method = "POST";

  //Get the request stream to save the Excel document stream
  Stream stream = req.GetRequestStream();

  //Write the Excel document stream into request stream
  stream.Write(inputStream.ToArray(), 0, inputStream.ToArray().Length);

  //Gets the responce from the Azure Functions.
  HttpWebResponse res = (HttpWebResponse)req.GetResponse();

  //Saves the image stream.
  FileStream outStream = File.Create("Sample.jpeg");
  res.GetResponseStream().CopyTo(outStream);

  //Dispose the streams
  inputStream.Dispose();
  outStream.Dispose();
}
catch (Exception ex)
{
    throw;
}

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.