How to add image into the center of an Excel cell in C#, VB.NET?
Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to add or insert an image into Excel worksheet and align the image to the center of an Excel cell using XlsIO in C# and VB.NET.
Steps to import an image and align it to the center of an Excel cell, programmatically:
- Create a new C# Windows application project with two buttons Create and Open.
Create new Windows application project
- Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install XlsIO NuGet package
- Include the following namespace in the Form1.cs file.
C#
using Syncfusion.XlsIO; using System.IO;
VB.NET
Imports Syncfusion.XlsIO Imports System.IO
- (a) Use the following code snippet to add an image to the center of an Excel cell in the newly created Excel document.
C#
//Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; //Create a workbook IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; //Set a marker to the worksheet to import an image with center alignment. worksheet["A1"].Text = "%Reports.Image;position:middle-center"; //Add an image to the center of a cell using template markers ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor(); FileStream imageStream = new FileStream("../../Data/Logo.png", FileMode.Open, FileAccess.ReadWrite); IList<Report> reports = GetSalesReports(imageStream); marker.AddVariable("Reports", reports); marker.ApplyMarkers(); //Save the workbook to disk in xlsx format workbook.SaveAs("../../Output/CreatedOutput.xlsx"); }
VB.NET
'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2016 'Create a workbook Dim workbook As IWorkbook = application.Workbooks.Create(1) Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Set a marker to the worksheet to import an image with center alignment. worksheet("A1").Text = "%Reports.Image;position:middle-center" 'Add an image to the center of a cell using template markers Dim marker As ITemplateMarkersProcessor = workbook.CreateTemplateMarkersProcessor() Dim imageStream As FileStream = New FileStream("../../Data/Logo.png", FileMode.Open, FileAccess.ReadWrite) Dim reports As IList(Of Report) = GetSalesReports(imageStream) marker.AddVariable("Reports", reports) marker.ApplyMarkers() 'Save the workbook to disk in xlsx format workbook.SaveAs("../../Output/CreatedOutput.xlsx") End Using
- (b) Use the following code snippet to add an image to the center of an Excel cell in the existing Excel document that contains template markers.
C#
//Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; //Open a workbook containing template marker IWorkbook workbook = application.Workbooks.Open("../../Data/Template.xlsx"); IWorksheet worksheet = workbook.Worksheets[0]; //Add an image to the center of a cell using template markers ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor(); FileStream imageStream = new FileStream("../../Data/Logo.png", FileMode.Open, FileAccess.ReadWrite); IList<Report> reports = GetSalesReports(imageStream); marker.AddVariable("Reports", reports); marker.ApplyMarkers(); //Save the workbook to disk in xlsx format workbook.SaveAs("../../Output/OpenedOutput.xlsx"); }
VB.NET
'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2016 'Open a workbook containing template marker Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/Template.xlsx") Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Add an image to the center of a cell using template markers Dim marker As ITemplateMarkersProcessor = workbook.CreateTemplateMarkersProcessor() Dim imageStream As FileStream = New FileStream("../../Data/Logo.png", FileMode.Open, FileAccess.ReadWrite) Dim reports As IList(Of Report) = GetSalesReports(imageStream) marker.AddVariable("Reports", reports) marker.ApplyMarkers() 'Save the workbook to disk in xlsx format workbook.SaveAs("../../Output/OpenedOutput.xlsx") End Using
- Add the following method GetSalesReports as well as Report class to convert the image from stream to image in array of bytes.
C#
private static List<Report> GetSalesReports(Stream stream) { List<Report> reports = new List<Report>(); reports.Add(new Report(stream)); return reports; } public class Report { public byte[] Image { get; set; } public Report(Stream stream) { Image = GetImage(stream); } private byte[] GetImage(Stream imageStream) { using (BinaryReader reader = new BinaryReader(imageStream)) { return reader.ReadBytes((int)imageStream.Length); } } }
VB.NET
Private Shared Function GetSalesReports(ByVal stream As Stream) As List(Of Report) Dim reports As List(Of Report) = New List(Of Report)() reports.Add(New Report(stream)) Return reports End Function Public Class Report Public Property Image As Byte() Public Sub New(ByVal stream As Stream) Image = GetImage(stream) End Sub Private Function GetImage(ByVal imageStream As Stream) As Byte() Using reader As BinaryReader = New BinaryReader(imageStream) Return reader.ReadBytes(CInt(imageStream.Length)) End Using End Function End Class
A complete Windows Forms working example of how to add an image to the center of an Excel cell using template markers in C# and VB can be downloaded from Position image in Excel cell.zip.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
See Also:
How to update formula for each row using Template Markers
What are the arguments in Template Markers and how to use them in XlsIO?
How do I insert an Image using XlsIO?
Create Excel comment with background image in C#, VB.NET
How to use size, position, fittocell arguments in template markers using XlsIO?
How to use template marker with collection object?
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 trial message.