My task is to put the image from the URL in a specific cell on an excel sheet. I am using NetOffice with C # for this.
My main problem is that I cannot find a way to insert the image exactly into the cell. When I use Sheet.Shapes.AddPicture (), I have to calculate the coordinates where to place the image. Of course, I have no problems with this (I created some kind of workaround), but I would like to ask if my approach to solving this problem is suitable or if there is some other method in which I could insert the image into the cell.
Here is my way:
var floatLeft = FloatLeftPixelsCalculation(rowNumber);
var floatTop = FloatTopPixelsCalculation(rowNumber);
Worksheet.Shapes.AddPicture(urlCellValue, MsoTriState.msoFalse, MsoTriState.msoTrue, floatLeft, floatTop, PictureWidth, PictureHeight);
public float FloatTopPixelsCalculation(int rowNumber)
{
float floatTop = 0;
for (var rNumber = 1; rNumber < rowNumber; rNumber++)
{
var cellHeight = Convert.ToSingle(Worksheet.Cells[rNumber, ColumnIndex].RowHeight);
floatTop = floatTop + cellHeight;
}
return floatTop;
}
public float FloatLeftPixelsCalculation(int rowNumber)
{
float floatLeft = 0;
for (var columnNumber = 1; columnNumber < ColumnIndex; columnNumber++)
{
var cellWidth = Convert.ToSingle(Worksheet.Cells[rowNumber, columnNumber].ColumnWidth);
floatLeft = floatLeft + cellWidth;
}
return floatLeft;
}
source
share