Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding images into Excel using EPPlus

Tags:

c#

epplus

I am trying to add the same image multiple times into an excel file using EPPlus. I am using the following code to do so:

Image logo = Image.FromFile(path); ExcelPackage package = new ExcelPackage(info); var ws = package.Workbook.Worksheets.Add("Test Page"); for(int a = 0; a < 5; a++) {     ws.Row(a*5).Height = 39.00D;     var picture = ws.Drawings.AddPicture(a.ToString(), logo);     picture.SetPosition(a*5, 0, 2, 0); } 

Everything works perfectly and all the images are correctly added but they are stretched downwards. Here is what one of the pictures should look like:

enter image description here

But it looks like this in excel:

enter image description here

I have to resize each row of the start of each picture but I dont think that would be affecting it. Would there be a way to add the pictures/do what I am trying to do or would I have to copy-paste the images in manually? (I am using the picture as an example)

Thanks.

like image 328
matthewr Avatar asked Jul 21 '12 01:07

matthewr


People also ask

How do you insert pictures into Excel?

Click the location in your worksheet where you want to insert a picture. On the Insert ribbon, click Pictures. Select Stock Images… Browse to the picture you want to insert, select it, and then click Open.

Does EPPlus work with XLS?

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.

What is the use of EPPlus?

EPPlus is a very helpful open-source 3rd party DLL for writing data to excel. EPPlus supports multiple properties of spreadsheets like cell ranges, cell styling, charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation, etc.


1 Answers

I'm not sure if this is the best solution but definetly a workaround for your problem.

Here's what I did:

ExcelPackage package = new ExcelPackage(); var ws = package.Workbook.Worksheets.Add("Test Page");  for (int a = 0; a < 5; a++) {     ws.Row(a * 5).Height = 39.00D; }  for (int a = 0; a < 5; a++) {     var picture = ws.Drawings.AddPicture(a.ToString(), logo);     picture.SetPosition(a * 5, 0, 2, 0); } 

Here is how it looks.

enter image description here

For some reason when we have the row height set, its interfering with the picture height.

like image 50
Jack7 Avatar answered Sep 23 '22 20:09

Jack7