easyexcel导出图片到具体excel具体位置并设置大小
提示:easyexcel模板导出,并导出图片到具体位置
前言
最近在做物料工艺单,用到easyexcel通过模板导出物料数据并在excel中具体位置插入尺寸图,研究了官方语雀文档,发现导出图片使用的是ImageData类封装一些图像位置信息和图像信息,但是搞来搞去发现老是会在最后一行第一格插入或者报什么转换异常之类的,达不到效果。后面通过研究源码,找到了一条路径。本文记录一下,供广大网友参考。
一、easyexcel源码中填充图片并设置大小的方法
AbstractExcelWriteExecutor 类 fillImage 方法
二、改造后的代码
// bytes为要导出的图像文件字节数组 bytes = outputStream.toByteArray(); if (bytes != null) { WriteContext writeContext = excelWriter.writeContext(); Workbook workbook = writeContext.writeWorkbookHolder().getWorkbook(); Sheet sheet = writeContext.writeSheetHolder().getCachedSheet(); Drawing<?> drawing = sheet.getDrawingPatriarch(); if (drawing == null) { drawing = sheet.createDrawingPatriarch(); } CreationHelper helper = sheet.getWorkbook().getCreationHelper(); int index = workbook.addPicture(bytes, FileTypeUtils.getImageTypeFormat(bytes)); ClientAnchor anchor = helper.createClientAnchor(); anchor.setDx2(-StyleUtil.getCoordinate(5)); anchor.setDx1(StyleUtil.getCoordinate(5)); // 图片位置设置代码 anchor.setRow1(StyleUtil.getCellCoordinate(0, 0, 4)); anchor.setCol1(StyleUtil.getCellCoordinate(0, 0, 7)); anchor.setRow2(StyleUtil.getCellCoordinate(0, 0, 16) + 1); anchor.setCol2(StyleUtil.getCellCoordinate(0, 0, 14) + 1); anchor.setAnchorType(ClientAnchorData.AnchorType.MOVE_AND_RESIZE.getValue()); drawing.createPicture(anchor, index); }
上一篇:
通过多线程提高代码的执行效率例子
下一篇:
Java用for循环打印九九乘法表