您的位置:首页 > 其它

POI插入图片至Excel使用固定的长宽

2017-08-10 09:09 405 查看
使用POI在Excel里插入图片,如何使插入的图片使用固定的大小?先介绍原有的两种方式:

  一种是指定开始和结尾单元格,然后从头画到尾,相当于平铺
  还有一种就是仅指定开始的单元格,图片的大小跟这个单元格的长宽有关,可以放大缩小固定的倍数,相当于左对齐

第一种效果如下:

/**
* Creates a new client anchor and sets the top-left and bottom-right
* coordinates of the anchor.
*
* Note: Microsoft Excel seems to sometimes disallow
* higher y1 than y2 or higher x1 than x2, you might need to
* reverse them and draw shapes vertically or horizontally flipped!
*
* @param dx1  the x coordinate within the first cell.
* @param dy1  the y coordinate within the first cell.
* @param dx2  the x coordinate within the second cell.
* @param dy2  the y coordinate within the second cell.
* @param col1 the column (0 based) of the first cell.
* @param row1 the row (0 based) of the first cell.
* @param col2 the column (0 based) of the second cell.
* @param row2 the row (0 based) of the second cell.
*/
public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) {
super(dx1, dy1, dx2, dy2);

checkRange(dx1, 0, 1023, "dx1");
checkRange(dx2, 0, 1023, "dx2");
checkRange(dy1, 0, 255, "dy1");
checkRange(dy2, 0, 255, "dy2");
checkRange(col1, 0, MAX_COL, "col1");
checkRange(col2, 0, MAX_COL, "col2");
checkRange(row1, 0, MAX_ROW, "row1");
checkRange(row2, 0, MAX_ROW, "row2");

setCol1((short) Math.min(col1, col2));
setCol2((short) Math.max(col1, col2));
setRow1(Math.min(row1, row2));
setRow2(Math.max(row1, row2));

if (col1 > col2){
_isHorizontallyFlipped = true;
}
if (row1 > row2){
_isVerticallyFlipped = true;
}
}


View Code

  原创文章,欢迎转载,转载请注明出处!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: