Search code examples
c#excelepplus

(C#) Can I programmatically set an XLSX cell to a picture/image?


I am hoping to make spreadsheets that contain some pictures (embed pictures from files) and I started looking at EPPlus (looks like a great library)

However it seems that the images are not tied to a cell - rather to an x,y, coordinate.

Is there a way with EPPlus or other way to set a cell to a picture (and then manipulate the size of the cell?)

SetPosition


Solution

  • You can insert the picture, then adjust its .Top and .Left so it aligns with the .Top and .Left of the appropriate cell. You can set the .RowHeight of the cell's row using the same units as the .height of the picture (though there's a maximum height). The .ColumnWidth of the column is in units of text characters wide, so what I do is something like:

    myColumn.ColumnWidth = myColumn.ColumnWidth / myColumn.Width * myPicture.Width
    

    and I run it twice because sometimes the first time you set .ColumnWidth, it isn't set precisely.