I am using Epplus library to generate Excel 2010 and up compatible files in Asp.Net C#. I am using version 3.1.2 which is the latest at this moment.
I am setting the row height first, before adding any pictures like this:
ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("sheet 1");
while (i < dt.Rows.Count + offset)
{
ws.Row(i).Height = 84;
i++;
}
dt
is my DataTable with DataRows.
After setting the height, I am looping again through the rows to add the pictures
while (i < dt.Rows.Count + offset)
{
var prodImg = ws.Drawings.AddPicture(dr["code"].ToString(), new FileInfo(path));
prodImg.SetPosition(i - 1, 0, 14, 0);
prodImg.SetSize(75);
}
This works, but this does not:
var prodImg = ws.Drawings.AddPicture(dr["code"].ToString(), new FileInfo(path));
int w = prodImg.Image.Width;
int h = prodImg.Image.Height;
if (h > 140) // because height of 84 is 140 pixels in excel
{
double scale = h / 140.0;
w = (int)Math.Floor(w / scale);
h = 140;
}
int xOff = (150 - w) / 2;
int yOff = (140 - h) / 2;
prodImg.SetPosition(i - 1, xOff, 11, yOff);
prodImg.SetSize(w, h);
This results in off center pictures and unresized images. And this code then which is in the same loop:
var prodImgDm = ws.Drawings.AddPicture("bcdm" + dr["code"].ToString(), new FileInfo(pathDm));
prodImgDm.SetPosition(i - 1, 25, 15, 40);
prodImgDm.SetSize(100);
This does work sometimes. the pictures prodImgDm
are datamatrix images with a static width and height and do not need to be resized because they are always small/tiny. So also without the SetSize
in some rows, it works and in some other rows, it does not work. Really strange because the code is the same. It might be something in the library and/or Excel. Perhaps I am using it wrong? Any epplus picture expert?
Thanks in advance!!
edit sometimes a picture is worth a thousand words, so here is the screenshot. As you can see the product images are not horizontal and vertical aligned in the cell. And the datamatrix on the far right is sometimes scaled about 120% even when I set SetSize(100)
so it is really strange to me. So the last datamatrix has the correct size... I already found this SO thread but that does not help me out, I think.
edit 2013/04/09 Essenpillai gave me a hint to set
pck.DoAdjustDrawings = false;
but that gave me even stranger images:
the datamatrix is still changing on row basis. on row is ok, the other is not. and the ean13 code is too wide.
I have the same problem with Epplus library.
After I find no solution how to solve this in my code, I checked source code of this library. Epplus create excel picture always as
twoCellAnchor
drawing. Inxlsx
files you can finddrawingXYZ.xml
with this code:So, picture is always connected to two cells, and this is not variable part of Epplus library. You can find this part of code in ExcelDrawing.cs file.
You can easy create your own copy of this dll. The good news is that you need to modify only two files to fix this problem. So..
Download your copy of source codes for Epplus library from this site and open in Visual Studio.
We need to generate code of
drawing
as oneCellAnchor, so we must remove<xdr:to>
element for pictures and create element<xdr:ext />
with picture dimensions as parameters.New xml structure will looks like:
Ok, so, how to do this?
Changes in Epplus code
ExcelDrawings.cs (link to file here)
CreateDrawingXml()
insideExcelDrawings.cs
. Order to preserve the original functionality we add an optional parameter (if createoneCellAnchor
) with default value. And in method, based this parameter, we create one or tow cell anchor and create or not<xdr:to>
element.Important part of this method code:
Then we modify two methods for
AddPicture
inside the same file:And this method with
FileInfo
as input parameter:So, this are all important code. Now we must change code for searching nodes and preserve order in elements.
In
private void AddDrawings()
we changexpath
from:To this:
It is all in this file, now we change
ExcelPicture.cs (link to file here)
Original code find node for append next code in constructor like this:
Because we do not create
<xdr:to>
element always, we change this code:And the same for second constructor with FileInfo as input parameter:
Now, pictures are created as
oneCellAnchor
. If you want, you can create multipleAddPicture
methods for booth variants. Last step is build this project and create your own customEPPlus.dll
. Then close your project which use this dll and copy new filesEPPlus.dll
,EPPlus.pdb
,EPPlus.XML
inside your project (backup and replace your original dll files) at the same place (so you don't need do any change in your project references or settings).Then open and rebuild your project and try if this solve your problem.
enter image description here
Maybe I am too late, but here is mine answer.. you can read it on codeplex issue as well (https://epplus.codeplex.com/workitem/14846)
I got this problem as well.
And after some research I figured out where the bug is.
It's in the ExcelRow class on the 149 line of code (ExcelRow.cs file).
There is a mistake, when row's Height got changed it recalcs all pictures heights but uses pictures widths inplace of heights, so it's easy to fix.
Just change the line
to
See the code changes on image
P.S. Actual for version 4.0.4