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

Free Support Forum - aspose.com

How to align an image in center of excel cell?

Hello Team,


Is it possible to align image in the center of the cell?

Regards,
Charles

I forgot to add that we are using Aspose Java Cells.

Hi Charles,


Thank you for contacting Aspose support.

Please note, you cannot center align image in Excel automatically, however, you can re-position the image manually in a such a way that it looks like in the center of the cell. In perspective of Aspose.Cells APIs, you can add image and adjust cell height and width so that the image looks like in the center of cell. Please check following piece of code and its resultant spreadsheet.

Java

//Create a workbook
Workbook workbook = new Workbook();
//Access first sheet
Worksheet worksheet = workbook.getWorksheets().get(0);
//Add a picture inside cell at this row and column
int picId = worksheet.getPictures().add(0, 0, dir + “image.png”);
Picture pic = worksheet.getPictures().get(picId);
//Set the height of the first row
worksheet.getCells().setRowHeightPixel(0, pic.getHeight()); //height equals to picture height
//Set the width of the first and second column
worksheet.getCells().setColumnWidthPixel(0, pic.getWidth());
//Save the file
workbook.save(dir+“output.xlsx”);