Home » excel » c# – Get the height and width of merged cells to place picture.

c# – Get the height and width of merged cells to place picture.

Posted by: admin May 14, 2020 Leave a comment


I am trying to auto size pictures i place into my excel to the size of the cell there in, but it gives me the original height and width and not the height and with of the whole merged Cells.

I am using Microsoft.Office.Interop.Excel to interact with my workbook and spreadsheets.

//This is checking if is a photo
if (File.Exists(ArrayNode[i].TagValue))
float Left, Top, Width, Height;
Left = float.Parse(cell.Left.ToString());
Top = float.Parse(cell.Top.ToString());
//This gives me the width and height of the cell, but i need it for the merged cells.
Width = float.Parse(cell.Width.ToString());
Height = float.Parse(cell.Height.ToString());
String path = Application.StartupPath + "\" + ArrayNode[i].TagValue;
xlWorkSheet[k].Shapes.AddPicture(path, Microsoft.Office.Core.MsoTriState.msoFalse,     Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, Width, Height); 
How to&Answers:

To work with merged cells to place the picture you have to get the .MergeArea.Width and .MergeArea.Height

For example

Width = float.Parse(cell.MergeArea.Width.ToString());
Height = float.Parse(cell.MergeArea.Height.ToString());