2011-06-02 68 views
5

我通過一些像這樣的代碼添加到工作表的圖像:POI的Excel HSSFPicture圖片和漿紗

// 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() ; 

但是添加圖像剛過,我調整列 - 這似乎把事情搞得一團糟。它調整圖像大小 - 這不是我想要的。

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

BrandedWorksheet v; 
v.autoSizeColumns() 

如果我不執行autoSizeColumns(),圖像的大小是合適的。

有什麼辦法可以兼得嗎?

+0

你可能不是先大小的列,然後添加圖像在你想要的大小? – Gagravarr 2011-06-02 16:35:16

+0

反向添加圖像的順序和自動調整大小沒有影響... – akaphenom 2011-06-02 17:36:58

回答

5

這太瘋狂了,沒有多大意義,但分配適當數量的間距和行似乎是關鍵。我添加了足夠的行,以便圖像可以在內部綁定到行 - 並且都很好。

道德故事:繼續工作和搞亂,最終可能會奏效。你會感到非常沮喪,並且在那裏沒有很多的幫助 - 因爲我們這些正確定位圖像的人不明白爲什麼。

+0

這是正確的。如果您將圖像大小設置爲低於圖像大小,圖像將被拉伸。如果你有足夠的空間,那麼它是完美的,圖像不會被拉伸。我假設我的列寬計算爲40。 – guerda 2012-03-28 15:14:15