Articles in this section
Category / Section

How to convert Excel to Image with Azure V3 function?

5 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can convert Excel documents into Images using Azure functions.

Steps to convert an Excel document to Image using Azure function:

1. Create a new Azure function project.

Create a new Azure function project

Create a new Azure function project

2. Name the project

Name the project

Name the project

3. Select Azure Functions v3 (.NET Core) and Http trigger.

Select Azure Functions v3 (.NET Core) and Http trigger

Select Azure Functions v3 (.NET Core) and Http trigger

4.Install the Syncfusion.XlsIORenderer.Net.Core NuGet package from NuGet.org.


Install the Syncfusion.XlsIORenderer.Net.Core NuGet package from NuGet.org

Install the Syncfusion.XlsIORenderer.Net.Core NuGet package from NuGet.org

5. Add the following namespaces in Function1.cs file.

C#

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Azure.WebJobs.Host;

 

6. Add the following code snippet in Run method of Function1.cs file.

C#

Stream stream = req.Content.ReadAsStreamAsync().Result;
//Instantiate the spreadsheet creation engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Excel2013;
    IWorkbook workbook = application.Workbooks.Open(File.OpenRead("../../Template.xlsx"), ExcelOpenType.Automatic);
    IWorksheet sheet = workbook.Worksheets[0];
 
    //Initialize XlsIORenderer 
    application.XlsIORenderer = new XlsIORenderer();
 
    //The stream to save the converted image 
    MemoryStream memoryStream = new MemoryStream();
 
    //Convert the worksheet’s used range to an image 
    sheet.ConvertToImage(sheet.UsedRange.Row, sheet.UsedRange.Column, sheet.UsedRange.LastRow, sheet.UsedRange.LastColumn, memoryStream);
 
    //Create the response to return
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
 
    //Set the PDF document content response
    response.Content = new ByteArrayContent(memoryStream.ToArray());
 
    //Set the contentDisposition as attachment
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "Output.png"
    };
    //Set the content type as PDF format mime type
    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
 
    //Return the response with output PDF stream
    return response;
}

 

7. Right-click on the project and select Publish.

Publish in Azure

Publish in Azure

8. Select Azure Function App.

Azure Function App

Azure Function App

9. Create App service using Azure Subscription.

Create App service using Azure Subscription

Create App service using Azure Subscription

10. After creating the profile, click on Publish.

After creating the profile, click on Publish

After creating the profile, click on Publish

11. Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it to the below client sample (Which will request the Azure Function, to convert Excel to Image with Azure functions). You will get the Image document as shown below.

Excel To Image

Excel To Image

A complete Azure Function sample can be downloaded from AzureFunction.Zip.

Steps to post the request to Azure function with template Excel document:

1. Create a simple console application to request the Azure function API.

2. Add the following code snippet into Main method to request the Azure function with template Excel document and get the converted Image document.

C#

//Open the required template Excel file
Stream fileStream = File.Open("../../Data/Template.xlsx", FileMode.Open);
 
//Create memory stream to save the template
MemoryStream inputStream = new MemoryStream();
 
//Copy the file stream into memory stream
fileStream.CopyTo(inputStream);
 
//Dispose the file stream
fileStream.Dispose();
try
{
    //Create HttpWebRequest with hosted azure function URL
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://azurefunction20210504163552.azurewebsites.net");
 
    //Set request method as POST
    req.Method = "POST";
 
    //Get the request stream to strore 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 Function request
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
 
    //Create file stream to save the output PDF file
    FileStream outStream = File.Create("Sample.png");
 
    //Copy the responce stream into file stream
    res.GetResponseStream().CopyTo(outStream);
 
    //Dispose the input stream
    inputStream.Dispose();
 
    //Dispose the file stream
    outStream.Dispose();
}
catch (Exception ex)
{
    throw;
}
 
//Launch the output document
System.Diagnostics.Process.Start("Sample.png");

 

The Console Application can be downloaded from ConsoleApp.Zip.

Know more about Syncfusion Excel (XlsIO) library through the documentation, where you can find features like charts , drawing objects , chart to image conversion and excel to pdf conversion etc. with respective code examples.

Refer here to explore the rich set of Syncfusion Excel (XlsIO) library 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 the 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 to Image with Azure V3 function.

You can refer to our .NET Excel library 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 .NET Excel library 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  feedbackportal. 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 sign in to leave a comment
Access denied
Access denied