-
Notifications
You must be signed in to change notification settings - Fork 306
Cell pictures
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 are added via the new Picture property a range.
The following image types are supported by EPPlus:
- png
- jpg
- gif
- bmp
- webp
- ico
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);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;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 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();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 aboveThe 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 Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles