We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Placing image at the center of a Excel file cell using Aspose.Cells for .NET in C#

Hi,
I am inserting a gif image into a cell using Pictures.Add method. But I am unable to center the image. It always posts on the top-left part of the cell. Do you have a solution?

I did a search on the forum and I found some work-arounds on posts almost 2 years old. So I was wondering if you had a solution by now. I am using Aspose 7.3 and I know it isn’t the latest, but if installing the latest (8.10 I believe) is going to solve this problem for me, then I can do it. But I don’t want to if it doesn’t.

Thanks,
Sreejith

Hi Sreejith,

Thanks for your posting and considering Aspose.Cells.

You cannot center align image in MS-Excel automatically. You will have to reposition it manually in a such a way that it looks like in center.

As a workaround in Aspose.Cells, you can add image and adjust cell height and width so that the image looks like in the center of cell.

Please see the following code for your reference. I have attached the output.xls file generated by it and the actual image and screenshot for to view.

C#


public static void AddPicture(Worksheet Sheet, int Row, int Column, int Width, int Height, string PicPath)

{

//Set the height of the first row

Sheet.Cells.SetRowHeightPixel(Row, Height); //height equals to picture height


//Set the width of the first and second column

Sheet.Cells.SetColumnWidthPixel(Column, Width);


//Add a picture inside cell at this row and column

int picId = Sheet.Pictures.Add(Row, Column, PicPath);

Picture pic = Sheet.Pictures[picId];


//Set the height and width of picture

pic.Height = Height;

pic.Width = Width;

}


void Run()

{

//Create a workbook

Workbook workbook = new Workbook();


//Access first sheet

Worksheet worksheet = workbook.Worksheets[0];


//Add a picture inside a cell

AddPicture(worksheet, 4, 4, 300, 200, @“image-koala.jpg”);


//Write the output

workbook.Save(@“output.xls”);

}