Skip to content

Cell pictures

Mats Alm edited this page Dec 6, 2024 · 7 revisions

EPPlus supports cell pictures - images inside a cell - from version 8, both local images and web images (images downloaded from an https url by the IMAGE function).

Local images

Local images are added via the new Picture property a range.

Supported image types

The following image types are supported by EPPlus:

  • png
  • jpg
  • gif
  • bmp
  • webp
  • ico

Add or replace a local picture

Note that the Set method only adds/replaces the image to one cell at the time. If you use it on a multi cell range the image will be set on the top-left cell.

// add via a byte array
var imageBytes = File.ReadAllBytes(@"c:\Temp\myImage.png");
worksheet.Cells["A1"].Picture.Set(imageBytes);
// add via a stream
var ms = new MemoryStream(imageBytes);
worksheet.Cells["A1"].Picture.Set(ms);
// or a file path
worksheet.Cells["A1"].Picture.Set(@"c:\Temp\myImage.png");
// or a FileInfo instance
var fileInfo = new FileInfo(@"c:\Temp\myImage.png");
worksheet.Cells["A1"].Picture.Set(fileInfo);

The Set method also has overloads where you can set the alt-text of the image and mark it as decorative (these properties are used by screenreaders).

worksheet.Cells["A1"].Picture.Set(imageBytes, "This is an alt text");
// or set decorative to true:
worksheet.Cells["A1"].Picture.Set(imageBytes, isDecorative: true);

Read a local picture

var pic = sheet.Cells["A1"].Picture.Get();
// get the file name (the internal name used in the workbook, could be something like "image3.png")
string fileName = pic.FileName;
// get the image bytes
byte[] imageBytes = pic.GetImageBytes();
// get the picture type (LocalImage or WebImage)
ExcelCellPictureTypes picType = pic.PictureType;
// get the alt-text
string altText = pic.AltText;
// get the cell's address
OfficeOpenXml.ExcelAddress address = pic.CellAddress;

Check if a local picture exists

if(sheet.Cells["A1"].Picture.Exists)
{
   Console.WriteLine("There is a picture in A1!");
}

Remove a local picture

sheet.Cells["A1"].Picture.Remove();
// you can also remove all pictures within a range with multiple cells
sheet.Cells["A1:C5"].Picture.Remove();

Local images in formula calculation

Local images are processed by the formula calculation in EPPlus. For example, this will add a local image in cell B1 and in cell C1.

// imageBytes is a byte array representing the image.
worksheet.Cells["A1"].Picture.Set(imageBytes);
worksheet.Cells["B1"].Formula = "A1";
worksheet.Cells["C1"].Formula = "IF(True(),A1,A2)";
worksheet.Calculate();

Local image cell value

If you access the Value property of a cell that contains a cell picture you will get an instance of the ExcelCellPicture class. This is the same class that is returned from the Picture.Get() method as described above.

Web images

Note that the IMAGE function is not supported on .NET 3.5. Web images can only be added by the IMAGE function, which is supported in the formula calculation from version 8.

// string url = "https://yoururl.com/yourimage.png"
sheet.Cells["A1"].Formula = $"IMAGE(\"{url}\")";
// the image will be downloaded and added to the cell during the calculation
sheet.Calculate();
var pic = sheet.Cells["A1"].Picture.Get();
// the picture can be read as the local image described above

IHttpService

The OfficeOpenXml.Interfaces.Net.IHttpService is a new interface available in the EPPlus.Interfaces NuGet package from version 8. It has only one method:

byte[] Download(string url);

By default, the IMAGE function in EPPlus uses an implementation of this interface to fetch images via HTTPS with no additional configuration. You can replace this with your own implementation of the IHttpService interface like this:

using var package = new ExcelPackage();
IHttpService myHttpsService = new MyOwnHttpsService();
package.Settings.ImageFunctionService = myHttpsService;

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally