POI插入图片至Excel使用固定的长宽 | 您所在的位置:网站首页 › 随单元格改变位置和大小 › POI插入图片至Excel使用固定的长宽 |
使用POI在Excel里插入图片,如何使插入的图片使用固定的大小?先介绍原有的两种方式: 一种是指定开始和结尾单元格,然后从头画到尾,相当于平铺 还有一种就是仅指定开始的单元格,图片的大小跟这个单元格的长宽有关,可以放大缩小固定的倍数,相当于左对齐 第一种效果如下: 第二种效果如下: 第一种方法的关键代码如下: 1 private void pictureToSheet(Sheet finalSheet, Row row, Cell cell, int pictureIdx) { 2 Drawing patriarch = finalSheet.createDrawingPatriarch(); 3 ExcelPositionRange excelPositionRange = ExcelTransferUtils.getMergedRegionPositionRange(finalSheet, row.getRowNum(), cell.getColumnIndex()); 4 ClientAnchor anchor = patriarch.createAnchor(0, 0, 1023, 255, 5 excelPositionRange.getFirstCol(), 6 excelPositionRange.getFirstRow(), 7 excelPositionRange.getLastCol(), 8 excelPositionRange.getLastRow() 9 ); 10 patriarch.createPicture(anchor, pictureIdx); 11 }注:代码中的excelPositionRange,是俺自定义的一个类型。里边只有四个变量和get/set方法,四个变量分别是单元格的开始、结尾单元格的横纵坐标。这个大家可以根据需要来改。 PS:其中1023和255指的是每个单元格被切分的份数,指定的是最后的单元格的最右下角的一个点,其方法的源代码在本文最后的附录里。
第二种方法的关键代码如下: 1 private void pictureToSheet(Sheet finalSheet, Row row, Cell cell, int pictureIdx) { 2 Drawing patriarch = finalSheet.createDrawingPatriarch(); 3 ExcelPositionRange excelPositionRange = ExcelTransferUtils.getMergedRegionPositionRange(finalSheet, row.getRowNum(), cell.getColumnIndex()); 4 5 CreationHelper helper = finalSheet.getWorkbook().getCreationHelper(); 6 ClientAnchor anchor = helper.createClientAnchor(); 7 8 // 图片插入坐标 9 anchor.setCol1(excelPositionRange.getFirstCol()); 10 anchor.setRow1(excelPositionRange.getFirstRow()); 11 12 // 使用固定的长宽比例系数 13 double a = 5.9; 14 double b = 1; 15 16 // 插入图片 17 Picture pict = patriarch.createPicture(anchor, pictureIdx); 18 pict.resize(a,b); 19 }
进阶方法: 在第二种方法的基础上,可以计算出不同的系数,达到生成图片都是同一个长宽的功能,从而输出固定大小的图片 1 private void pictureToSheet(Sheet finalSheet, Row row, Cell cell, int pictureIdx) { 2 Drawing patriarch = finalSheet.createDrawingPatriarch(); 3 ExcelPositionRange excelPositionRange = ExcelTransferUtils.getMergedRegionPositionRange(finalSheet, row.getRowNum(), cell.getColumnIndex()); 4 5 CreationHelper helper = finalSheet.getWorkbook().getCreationHelper(); 6 ClientAnchor anchor = helper.createClientAnchor(); 7 8 // 图片插入坐标 9 anchor.setCol1(excelPositionRange.getFirstCol()); 10 anchor.setRow1(excelPositionRange.getFirstRow()); 11 12 // 指定我想要的长宽 13 double standardWidth = 112; 14 double standardHeight = 41; 15 16 // 计算单元格的长宽 17 double cellWidth = finalSheet.getColumnWidthInPixels(cell.getColumnIndex()); 18 double cellHeight = cell.getRow().getHeightInPoints()/72*96; 19 20 // 计算需要的长宽比例的系数 21 double a = standardWidth / cellWidth; 22 double b = standardHeight / cellHeight; 23 24 // 插入图片 25 Picture pict = patriarch.createPicture(anchor, pictureIdx); 26 pict.resize(a,b); 27 }PS:这里参考了POI获取单元格长宽的的方法:http://www.cnblogs.com/acm-bingzi/p/poiWidth.html
附录一 一般插入图片的样例代码: 1 // 插入 PNG 图片至 Excel 2 String fileName = strAppRootPath + "images/" + "bxlogo.png"; 3 4 InputStream is = new FileInputStream(fileName); 5 byte[] bytes = IOUtils.toByteArray(is); 6 7 int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); 8 9 CreationHelper helper = workbook.getCreationHelper(); 10 Drawing drawing = sheet.createDrawingPatriarch(); 11 ClientAnchor anchor = helper.createClientAnchor(); 12 13 // 图片插入坐标 14 anchor.setCol1(0); 15 anchor.setRow1(1); 16 // 插入图片 17 Picture pict = drawing.createPicture(anchor, pictureIdx); 18 pict.resize();
附录二 patriarch.createAnchor的源代码跟踪 ![]() ![]()
原创文章,欢迎转载,转载请注明出处! |
CopyRight 2018-2019 实验室设备网 版权所有 |