1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java Excel图片在合并单元格等比缩放且居中显示

java Excel图片在合并单元格等比缩放且居中显示

时间:2021-09-21 19:24:13

相关推荐

java Excel图片在合并单元格等比缩放且居中显示

下面展示一些内联代码片

int x = 9; //列总数int y = 8; //行总数//单元格高度double heightRow = (cell.getRow().getHeightInPoints() / 72) * 96; //80//单元格宽度double widthRow = sheetPicture.getColumnWidth(cell.getColumnIndex()) / 32; //86byte[] fileContentBmp = SystemUtil.fileConvertToByteArray("D:/a1.png"); 测试if(fileContentBmp.length <= 0){ //没图片不处理break;}InputStream in = new ByteArrayInputStream(fileContentBmp);BufferedImage ms = ImageIO.read(in);double imageOriginalWidth = ms.getWidth();//原始图片的长度double imageOriginalHeight = ms.getHeight();//原始图片的宽度double allHeight = heightRow * y; //合并单元格总高double allWidth = widthRow * x; //合并单元格总宽/*** 1.把高满铺,计算宽是否超出* 2.不超出,计算宽占得格总数,在中间画* 3.超出,那么把宽满铺,计算高的格子总数,在中间画*/HSSFClientAnchor anchor = null;double proportion= allHeight / imageOriginalHeight; //比例double estimateWidth = imageOriginalWidth * proportion; // 高满铺时要求宽那么多if(estimateWidth <= allWidth){ //2.不超出,计算宽占得格总数,在中间画double actWidth = allHeight*(imageOriginalWidth / imageOriginalHeight);double actWidthCount = actWidth / widthRow; //需要的行数double side = (x - actWidthCount) / 2;int sideInt = (int) Math.round(side);anchor = new HSSFClientAnchor(10, 10, 1000, 250,(short) (cell.getColumnIndex()+sideInt), cell.getRowIndex(), //开始坐标(short) ((short) cell.getColumnIndex() + x-sideInt-1), cell.getRowIndex() + y-1); // 结束坐标获取不到,只能累加}else { //3.超出,那么把宽满铺,计算高的格子总数,在中间画double actHeight = allWidth/(imageOriginalWidth / imageOriginalHeight);double actHeightCount = actHeight / heightRow; //需要的行数double top = (y-actHeightCount)/2;int topInt = (int) Math.round(top);anchor = new HSSFClientAnchor(10, 10, 1000, 250,(short) (cell.getColumnIndex()), cell.getRowIndex()+topInt, //开始坐标(short) ((short) cell.getColumnIndex() + x-1), cell.getRowIndex() + y-topInt-1); // 结束坐标获取不到,只能累加}// 插入图片HSSFPatriarch patriarch = sheetPicture.createDrawingPatriarch();patriarch.createPicture(anchor, workbook.addPicture(fileContentBmp,HSSFWorkbook.PICTURE_TYPE_JPEG));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。