-
Notifications
You must be signed in to change notification settings - Fork 287
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.
// 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);
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;
if(sheet.Cells["A1"].Picture.Exists)
{
Console.WriteLine("There is a picture in A1!");
}
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();
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.
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
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 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
- 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
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles