【EasyExcel等比例缩小导出图片】

EasyExcel等比例缩小导出图片

一、背景

  • 使用EasyExcel导出excel文件,但是需要同时导出图片信息,且图片信息不能影响行高和单元格宽度,图片本身被导出时,不能因为压缩导致图片变形

二、思路

  • 使用EasyExcel最主要是要实现各种hadler,通过实现CellWriteHandler,获取单元格图片,进行图片压缩;
  • 由于excel的行高和单元格宽度换算方式完全不一样,所以要通过计算得出最合适的压缩比例
  • (excel宽度 / 字符宽度转换为标准字符宽度) * 估算的字符到像素的转换因子 = 宽度像素;
  • (excel高度 / 点转换为英寸) * 英寸转换为像素 = 行高像素;
  • 最终宽度像素或者行高像素 根据行高与像素的转换因子 ,计算出需要设置的单元格间距值,即可正常缩小图片

三、代码

java 复制代码
public static class QuotationCustomCellWriteHandler implements CellWriteHandler {
        // 256分之一的字符宽度转换为标准字符宽度。
        public static final int STANDARD_CHARACTER_WIDTH = 256;
        // 7.5是一个估算的字符到像素的转换因子
        public static final float CHARACTER_2_PIXEL_FACTOR = 7.5f;
        // 将点转换为英寸,因为1点 = 1/72英寸。
        public static final int PIXEL_2_INCH_FACTOR = 72;
        // 英寸转换为像素,其中96是常用的DPI(每英寸像素数)值。
        public static final int DPI = 96;
        // 行高与像素的转换因子
        public static final float ROW_HEIGHT_2_PIXEL_FACTOR = 1.3333f;

        /**
         * 外界传入的行高,内部查不到
         */
        private final int rowHeight;

        public QuotationCustomCellWriteHandler(int rowHeight) {
            this.rowHeight = rowHeight;
        }

        @Override
        public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                           WriteCellData<?> cellData, Cell cell, Head head, Integer relativeRowIndex,
                                           Boolean isHead) {
            boolean noImageValue = Objects.isNull(cellData) || cellData.getImageDataList() == null || cellData.getImageDataList().isEmpty();
            if (Objects.equals(Boolean.TRUE, isHead) || noImageValue) {
                return;
            }
            Sheet sheet = cell.getSheet();
            ImageData imageData = cellData.getImageDataList().get(0);
            imageData.setRelativeLastRowIndex(0);
            imageData.setRelativeLastColumnIndex(0);
            // 处理图像缩放 - 等比例缩放
            try (ByteArrayInputStream bis = new ByteArrayInputStream(imageData.getImage())) {
                BufferedImage image = ImageIO.read(bis);
                int targetWidth = (int) ((double) sheet.getColumnWidth(cell.getColumnIndex()) / STANDARD_CHARACTER_WIDTH * CHARACTER_2_PIXEL_FACTOR);
                int targetHeight = (int) (rowHeight * 1.0 / PIXEL_2_INCH_FACTOR * DPI);

                // 计算图像的缩放比例
                double scaleX = (double) targetWidth / image.getWidth();
                double scaleY = (double) targetHeight / image.getHeight();
                double scale = Math.min(scaleX, scaleY);

                // 计算缩放后的图像大小
                int scaledWidth = (int) (image.getWidth() * scale);
                int scaledHeight = (int) (image.getHeight() * scale);

                // 计算上下左右四个角的空白
                int topPadding = (targetHeight - scaledHeight) / 2;
                int bottomPadding = targetHeight - scaledHeight - topPadding;
                int leftPadding = (targetWidth - scaledWidth) / 2;
                int rightPadding = targetWidth - scaledWidth - leftPadding;

                // 行高(点)= 像素高度 / 1.3333
                imageData.setTop((int) (topPadding / ROW_HEIGHT_2_PIXEL_FACTOR));
                imageData.setBottom((int) (bottomPadding / ROW_HEIGHT_2_PIXEL_FACTOR));
                imageData.setLeft((int) (leftPadding / ROW_HEIGHT_2_PIXEL_FACTOR));
                imageData.setRight((int) (rightPadding / ROW_HEIGHT_2_PIXEL_FACTOR));
            } catch (Exception e) {
                log.error("QuotationCustomCellWriteHandler afterCellDataConverted err", e);
            }
            CellWriteHandler.super.afterCellDataConverted(writeSheetHolder, writeTableHolder, cellData, cell, head, relativeRowIndex, isHead);
        }
    }
相关推荐
厦门德仔3 分钟前
【WPF】WPF(样式)
android·java·wpf
大春儿的试验田4 分钟前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
Gappsong8747 分钟前
【Linux学习】Linux安装并配置Redis
java·linux·运维·网络安全
hqxstudying11 分钟前
Redis为什么是单线程
java·redis
RainbowSea22 分钟前
NVM 切换 Node 版本工具的超详细安装说明
java·前端
逆风局?24 分钟前
Maven高级——分模块设计与开发
java·maven
周某某~26 分钟前
maven详解
java·maven
读书点滴27 分钟前
笨方法学python -练习14
java·前端·python
lingRJ77729 分钟前
微服务架构下的抉择:Consul vs. Eureka,服务发现该如何选型?
java·eureka·springcloud·consul·backend·microservices·servicediscovery
RainbowSea29 分钟前
问题:后端由于字符内容过长,前端展示精度丢失修复
java·spring boot·后端