Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POI Excel HSSFPicture Image and Sizing

I am adding an image to the worksheet through some code like this:

// Create the drawing patriarch.  This is the top level container for all shapes.
Drawing drawing = sheet.createDrawingPatriarch();

//add a picture shape
//ClientAnchor anchor = this.creationHelper.createClientAnchor();
ClientAnchor anchor = new HSSFClientAnchor((short)0, (short)0, (short)0, (short)0, (short)0, (short)0, (short)2, (short)5)

// 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells. 
anchor.setAnchorType(2)

HSSFPicture pict = drawing.createPicture(anchor, this.images.get("logo"));
pict.resize() ;

However just after the images are added, I resize the columns - which seems to mess things up. It resizes the images - which is not what I want.

//psuedo code
public void autoSizeColumns() {
    def cols = (StartColumn..this.cMax)
    cols.each { i ->
        sheet.autoSizeColumn i
    }   
}

BrandedWorksheet v; 
v.autoSizeColumns()

If I don't perform the autoSizeColumns() the image is the proper size.

Is there any way to have both?

like image 658
akaphenom Avatar asked Jun 02 '11 15:06

akaphenom


People also ask

What is poi in Excel?

POI stands For “Poor Obfuscation Implementation”. Apache POI is an API provided by Apache foundation which is a collection of different java libraries. These libraries gives the facility to read, write and manipulate different Microsoft files such as excel sheet, power-point, and word files.

How do I change the width of a column in Excel using poi?

If you set a column width to be eight characters wide, e.g. setColumnWidth(columnIndex, 8*256) , then the actual value of visible characters (the value shown in Excel) is derived from the following equation: Truncate([numChars*7+5]/7*256)/256 = 8; which gives 7.29 .


1 Answers

This is crazy and doesn't make much sense, but allocating the proper amount of spacing and rows seems to be the key. I added enough rows so that the image could be bound internally to the rows - and all seems good.

Moral of the story: keep working and messing about and it may eventually work. You will get really frustrated, and there won't be a ton of help out there - because those of us who have properly sized an image don't really understand why.

like image 128
akaphenom Avatar answered Sep 19 '22 23:09

akaphenom