Inserting images in Excel files is a very commonly used feature for Microsoft Excel users. Adding images to your Excel workbooks helps clarify your point of view and data representation more effectively. You can add multiple images to each worksheet in your workbook. As a .NET application developer, you may want to add the feature of inserting images in Excel files from within your application programmatically. The good news is that this can be achieved using NPOI API in C# and VB.NET as explained in this article.
In our series of articles for working with NPOI in .NET, you can find information about:
- Creating, saving, and loading an Excel file using NPOI in C#
- Adding a Comment to an Excel file using NPOI in C#
- Adding a Formula to an Excel file using NPOI in C#
In this article, we’ll show how to insert images into an Excel file with NPOI using C#/VB.NET. But before that, let us have a look at how to add an image to a Workbook using Microsoft Excel.
Insert Images in a Spreadsheet using Microsoft Excel
Microsoft Excel allows adding images to an Excel workbook. Whether it is the latest version of Microsoft Excel installed on your machine, an older version or you are using Microsoft Office 365, all these allow you to insert images in Excel files.
Steps to Insert Image in Spreadsheet with Microsoft Excel
If you want to add an image to an Excel workbook, you can use the following steps:
- In Microsoft Excel, go to Insert Menu
- Select Pictures and use the Open File or This Device option (whichever is available)
- From the open image dialog, select the image that you want to insert into your Excel file
- Once loaded, the image will be inserted into your worksheet
Insert Image in Excel Spreadsheet using NPOI in C#
Now that we have seen how to insert an image in a spreadsheet file using Microsoft Excel, we’ll now have a look at how we can do the same using NPOI in C#. But before that, we need to install NPOI in our .NET project to get started. You can learn about installing NPOI in your project in the detailed instructions guide for Installing NPOI for .NET.
Insert Image in Excel Spreadsheet using C#
Now that your development environment is ready for working with NPOI API in C#, we can create a console-based project and add the following lines of code to it.
//Load workbook IWorkbook wb = new XSSFWorkbook(new FileStream("FileFormat.com - Insert Image using NPOI.xlsx", FileMode.Open)); //Get the first sheet ISheet ws = wb.GetSheetAt(0); //Add picture data to the workbook byte bytes = File.ReadAllBytes("file-types.jpeg"); wb.AddPicture(bytes, PictureType.JPEG); //Add a picture shape and set its position IDrawing drawing = ws.CreateDrawingPatriarch(); IClientAnchor anchor = wb.GetCreationHelper().CreateClientAnchor(); anchor.Dx1 = 0; anchor.Dy1 = 0; anchor.Col1 = 5; anchor.Row1 = 5; IPicture picture = drawing.CreatePicture(anchor, 0); //Automatically adjust the image size picture.Resize(); //Save the file FileStream file = File.Create("Image In Excel.xlsx"); wb.Write(file, false); file.Close();
In this article, we showed how to insert images in an Excel workbook using NPOI API. The sample code is written in C#, but can also be re-written with VB.NET and it should work equally. If you would like to know more about working with Excel workbooks using NPOI, stay tuned for more examples in this section.