Add picture into named range cells in .NET

Hi,


I would like to add a picture in a ranged name but i don t know how to calculate the size (width and height) of it. The ratio of the image must be respected.

The range is on the top left corner of the sheet. It is called “LogoSociete”.

Here is the code i use :
range = workbook.Worksheets.GetRangeByName(“LogoSociete”);

// Calculate the size of the range

int j = worksheet.Pictures.Add(range.FirstRow, range.FirstColumn, pngStream);

Picture pic = worksheet.Pictures[j];

pic.Name = “test”;

pic.Width = // Width of the range;

pic.Height = // Height of the range

pic.IsLockAspectRatio = true;

pic.Placement = PlacementType.Move;

pic.IsLocked = true;
Can you help me ?

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version: Aspose.Cells
for .NET v7.3.4.3


You will have to calculate the columns size and row heights of your named range cells.

Please use the following methods to achieve these things.

  1. worksheet.Cells.GetColumnWidth()
  2. worksheet.Cells.GetColumnWidthInch()
  3. worksheet.Cells.GetColumnWidthPixel()
  4. worksheet.Cells.GetRowHeight()
  5. worksheet.Cells.GetRowHeightInch()
  6. worksheet.Cells.GetRowHeightPixel()

Hi,


I have already tried these methods.
The range (“LogoSociete”) object contains only one row(RowCount) and one column(ColumnCount)
But, if you look at the Excel sheet, there are many rows and columns …

range = workbook.Worksheets.GetRangeByName(“LogoSociete”);
Worksheet worksheet = range.Worksheet;

int height = 0;
int width = 0;
for (int row = range.FirstRow; row < range.RowCount; row++)
{
height += worksheet.Cells.GetRowHeightPixel(row);
}
for (int col = range.FirstColumn; col < range.ColumnCount; col++)
{
width += worksheet.Cells.GetColumnWidthPixel(col);
}
The results are 2(width) and 2(height). It is not correct

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

I have looked into your issue further with more details. It is quite cumbersome to add a picture inside your one range consisting of one cell and because Aspose.Cells does return the actual column width and row height instead of height and width inside the current view pane.

So, it returns 2 px for cell LogoSociete for both column and height but when I set the picture height 2 pixels then it is totally invisible.

As a workaround and easier solution, I have inserted a template picture inside your source xlsm file and updated with another picture on runtime. You can use this approach and get rid of calculating the rows and column heights and width altogether.

Please see the following code. I have attached the source and output xlsm files as well as screenshots for your reference.

C#

string filePath = @“F:\Shak-Data-RW\Downloads\Modele.xlsm”;

string picPath = @“D:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg”;

Workbook workbook = new Workbook(filePath);

Worksheet worksheet = workbook.Worksheets[0];

Picture pic = worksheet.Pictures[0];

pic.Data = File.ReadAllBytes(picPath);

workbook.Save(filePath + “.out.xlsm”);