ashish gupta
ashish gupta

Reputation: 41

How to centrally align an Image in Excel using apache poi

HSSFPatriarch drawing = sheet.createDrawingPatriarch();
HSSFClientAnchor my_anchor = (HSSFClientAnchor) helper.createClientAnchor();
my_anchor.setAnchorType(HSSFClientAnchor.DONT_MOVE_AND_RESIZE);
my_anchor.setCol1(0);
// my_anchor.
my_anchor.setRow1(excelData.getRowNum());
strb.append("  ");
HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);

/* Call resize method, which resizes the image */
my_picture.resize();

I am passing sheet, helper as parameter to my method.

With this code, still Image Icon can be moved in the excel sheet. Also I want to set the vertical alignment for the icon in the cell as bottom aligned. Please suggest.

Upvotes: 3

Views: 3508

Answers (2)

Yogesh
Yogesh

Reputation: 1

You can align the image using anchor, adjust the values of Dx1, Dy1, Dx2 and Dy2, pict.resize() accordingly. Below is the example:

ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor();
Modify below values s per your original image size
anchor.setDx1(0);
anchor.setDy1(0);
anchor.setDx2(90);
anchor.setDy2(90);
// Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
// Reset the image to the original size
Modify below value s per your original image size
pict.resize(1.75);

Please refer updated below URL for complete example: https://svn.apache.org/repos/asf/poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java

Upvotes: -1

Abhishek K
Abhishek K

Reputation: 673

You can align the image using anchor, adjust the values of Dx1, Dy1, Dx2 and Dy2 accordingly. Below is the example:-

ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor();
anchor.setDx1(0);
anchor.setDy1(0);
anchor.setDx2(0);
anchor.setDy2(0);

Refer to the below URL for complete example:- https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java

Upvotes: 1

Related Questions