【JAVA】easyexcel 导出excel文件带多个图片
短信预约 -IT技能 免费直播动态提醒
最终效果
pom版本
com.alibaba easyexcel 3.0.5
实现代码
package com.alibaba.easyexcel.test.demo.write;import com.alibaba.easyexcel.test.util.TestFileUtil;import com.alibaba.excel.EasyExcel;import com.alibaba.excel.metadata.data.ClientAnchorData;import com.alibaba.excel.metadata.data.ImageData;import com.alibaba.excel.metadata.data.WriteCellData;import com.alibaba.excel.util.FileUtils;import com.alibaba.excel.write.style.column.AutoColumnWidthStyleStrategy;import org.apache.commons.collections4.CollectionUtils;import org.junit.Test;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.atomic.AtomicReference;public class ImageExportTest { @Test public void imageWrite() throws Exception { //文件最后生成的路径 String fileName = TestFileUtil.getPath() + "imageWrite.xlsx"; System.out.println(fileName); //测试用的图片路径 String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; String imagePath2 = TestFileUtil.getPath() + "converter" + File.separator + "img2.jpeg"; //数据列表Demo List demoDataList = new ArrayList<>(); //图片路径 List path1 = new ArrayList<>(); path1.add(imagePath); List path2 = new ArrayList<>(); path2.add(imagePath); path2.add(imagePath2); List path3 = new ArrayList<>(); path3.add(imagePath); path3.add(imagePath); path3.add(imagePath2); List path4 = new ArrayList<>(); path4.add(imagePath); path4.add(imagePath2); path4.add(imagePath); path4.add(imagePath2); demoDataList.add(new ImageDemoData("王离", path1)); demoDataList.add(new ImageDemoData("杨开", path2)); demoDataList.add(new ImageDemoData("欧阳微微", path3)); demoDataList.add(new ImageDemoData("上官婉儿", path4)); genImageExcel(demoDataList, fileName); } private void genImageExcel(List demoDataList, String fileName) { if (CollectionUtils.isEmpty(demoDataList)) { return; } //图片列最大图片数 AtomicReference maxImageSize = new AtomicReference<>(0); demoDataList.forEach(item -> { if (CollectionUtils.isNotEmpty(item.getImagePathList()) && item.getImagePathList().size() > maxImageSize.get()) { maxImageSize.set(item.getImagePathList().size()); } }); //设置列长度所用类 AutoColumnWidthStyleStrategy longWidth = new AutoColumnWidthStyleStrategy(); demoDataList.forEach(item -> { WriteCellData writeCellData = new WriteCellData<>(); if (CollectionUtils.isNotEmpty(item.getImagePathList())) { //每张图片间距 Integer splitWidth = 2; //每张图片的长度 Integer imageWidth = 80; //图片列的最大长度 Integer sumWidth = maxImageSize.get() * (imageWidth + splitWidth); List imageDataList = new ArrayList<>(); List imagePathList = item.getImagePathList(); for (int i = 1; i <= imagePathList.size(); i++) { String path = imagePathList.get(i - 1); Integer left = imageWidth * (i - 1) + i * splitWidth; Integer right = sumWidth - imageWidth - left; ImageData imageData = new ImageData(); try { imageData.setImage(FileUtils.readFileToByteArray(new File(path))); } catch (IOException e) { e.printStackTrace(); } imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG); //距离单元格顶部距离 imageData.setTop(1); //距离单元格底部距离 imageData.setBottom(1); //距离单元格左边距离 imageData.setLeft(left); //距离单元格右边距离 imageData.setRight(right); imageData.setAnchorType(ClientAnchorData.AnchorType.DONT_MOVE_DO_RESIZE); imageDataList.add(imageData); } writeCellData.setImageDataList(imageDataList); Map zdyColumnWidth = new HashMap<>(); //图片列名称,对应导出对象的列名称,图片列长度 zdyColumnWidth.put("上传图片", sumWidth / 6); longWidth.setZdyColumnWidth(zdyColumnWidth); } item.setWriteCellDataFile(writeCellData); }); //写入数据 EasyExcel.write(fileName, ImageDemoData.class).registerWriteHandler(longWidth).sheet().doWrite(demoDataList); }}
package com.alibaba.easyexcel.test.demo.write;import com.alibaba.excel.annotation.ExcelIgnore;import com.alibaba.excel.annotation.ExcelProperty;import com.alibaba.excel.annotation.write.style.ContentRowHeight;import com.alibaba.excel.metadata.data.WriteCellData;import lombok.EqualsAndHashCode;import lombok.Getter;import lombok.Setter;import java.util.List;@Getter@Setter@EqualsAndHashCode@ContentRowHeight(40)public class ImageDemoData { @ExcelProperty(value = "用户名称") private String test; @ExcelIgnore private List imagePathList; @ExcelProperty(value = "上传图片") private WriteCellData writeCellDataFile; public ImageDemoData() { } public ImageDemoData(String test, List imagePathList) { this.test = test; this.imagePathList = imagePathList; } public ImageDemoData(List imagePathList) { this.imagePathList = imagePathList; }}
package com.alibaba.excel.write.style.column;import com.alibaba.excel.enums.CellDataTypeEnum;import com.alibaba.excel.metadata.Head;import com.alibaba.excel.metadata.data.WriteCellData;import com.alibaba.excel.util.MapUtils;import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;import org.apache.commons.collections4.CollectionUtils;import org.apache.poi.ss.usermodel.Cell;import java.util.HashMap;import java.util.List;import java.util.Map;public class AutoColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { private static final int MAX_COLUMN_WIDTH = 255; //自定义列的列宽 private Map zdyColumnWidth = MapUtils.newHashMapWithExpectedSize(2); private final Map> cache = MapUtils.newHashMapWithExpectedSize(8); @Override protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList); if (!needSetWidth) { return; } if (zdyColumnWidth.containsKey(cell.toString())) { writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), zdyColumnWidth.get(cell.toString()) * 256); return; } Map maxColumnWidthMap = cache.get(writeSheetHolder.getSheetNo()); if (maxColumnWidthMap == null) { maxColumnWidthMap = new HashMap(16); cache.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); } Integer columnWidth = dataLength(cellDataList, cell, isHead); if (columnWidth < 0) { return; } if (columnWidth > MAX_COLUMN_WIDTH) { columnWidth = MAX_COLUMN_WIDTH; } Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex()); if (maxColumnWidth == null || columnWidth > maxColumnWidth) { maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth); writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256); } } private Integer dataLength(List> cellDataList, Cell cell, Boolean isHead) { if (isHead) { return cell.getStringCellValue().getBytes().length; } WriteCellData> cellData = cellDataList.get(0); CellDataTypeEnum type = cellData.getType(); if (type == null) { return -1; } switch (type) { case STRING: return cellData.getStringValue().getBytes().length; case BOOLEAN: return cellData.getBooleanValue().toString().getBytes().length; case NUMBER: return cellData.getNumberValue().toString().getBytes().length; default: return -1; } } public void setZdyColumnWidth(Map zdyColumnWidth) { this.zdyColumnWidth = zdyColumnWidth; }}
来源地址:https://blog.csdn.net/xiaosemei/article/details/127671561
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341