Skip to content

Cell pictures

Mats Alm edited this page Dec 6, 2024 · 11 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.

// imageBytes is a byte array representing the image.
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
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;

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();

Web images

Note that the IMAGE function is not supported on .NET 3.5. Web images are added by the IMAGE function, which is supported in the formula calculation.

// 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 that is available in the EPPlus.Interfaces Nuget package from version 8. Per default the IMAGE function in EPPlus uses a web request with no configuration to fetch the image via https. You can replace this with your own implementation that implements this 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