最近在excel2000的版本上用POI插入图片的时候,即使调用resize方法,插入的图片还是走形了。
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_PNG).resize(1.0)
最近只好跟进POI的代码中debug。发现是宽度计算的对。
public HSSFClientAnchor getPreferredSize(double scale){
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
Dimension size = getImageDimension();
double scaledWidth = size.getWidth() * scale;
double scaledHeight = size.getHeight() * scale;
float w = 0;
//space in the leftmost cell
w += getColumnWidthInPixels(anchor.col1)*(1 - (float)anchor.dx1/1024);
short col2 = (short)(anchor.col1 + 1);
int dx2 = 0;
while(w < scaledWidth){
w += getColumnWidthInPixels(col2++);
}
if(w > scaledWidth) {
//calculate dx2, offset in the