前言
站内有很多基于2.2.7版本的文章,有些可能对使用更高版本不适用,也可能是我比较菜,在使用若依框架时套用对应的例子总是会有各种bug。在查看官方文档之后,发现有更方便的工具可以使用
官方文档:写Excel | Easy Excel 官网 (alibaba.com)
正文
官方给出的能够处理的图片文件的形式,本文以byte[]作为演示
@Getter
@Setter
@EqualsAndHashCode
@ContentRowHeight(100)
@ColumnWidth(100 / 8)
public class ImageDemoData {
private File file;
private InputStream inputStream;
/**
* 如果string类型 必须指定转换器,string默认转换成string
*/
@ExcelProperty(converter = StringImageConverter.class)
private String string;
private byte[] byteArray;
/**
* 根据url导出
*
* @since 2.1.1
*/
private URL url;
/**
* 根据文件导出 并设置导出的位置。
*
* @since 3.0.0-beta1
*/
private WriteCellData<Void> writeCellDataFile;
}
版本
Controller
/**
* 导出
*/
@SaCheckPermission("hg:postInfo:export")
@Log(title = "岗位信息", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(SahPostInfoBo bo, HttpServletResponse response) {
sahPostInfoService.export(bo, response);
}
Service
/**
* 导出
*
* @param bo
* @param response
*/
@Override
public void export(SahPostInfoBo bo, HttpServletResponse response) {
try {
//获取需要导出的数据信息
LambdaQueryWrapper<SahPostInfo> lqw = buildQueryWrapper(bo);
List<SahPostInfoVoExcel> sahPostInfoVos = baseMapper.selectVoList(lqw, SahPostInfoVoExcel.class);
for (SahPostInfoVoExcel sahPostInfoVo : sahPostInfoVos) {
//工作说明文件(一个单元格内一张图)
//图片存储在数据的ossId
Long supportingPaper = sahPostInfoVo.getSupportingPaper();
if (supportingPaper != null) {
SysOssVo sysOss = iSysOssService.getById(supportingPaper);
if (ObjectUtil.isNull(sysOss)) {
throw new ServiceException("文件数据不存在!");
}
//这里是获取文件数据,我这里使用的是对象存储,可以按照需求改成自己的
OssClient client = OssFactory.instance(sysOss.getService());
String ossFileName = sysOss.getFileName();
if (StringUtils.isNotBlank(ossFileName)){
//文件的字节数组(easyExcel可以处理很多种文件流格式,按照自己需求)
byte[] download = client.download(ossFileName);
//将文件数据存在视图对象的指定字段
sahPostInfoVo.setSupportingPaperInput(download);
}
}
}
//证书(一个单元格内多张图)
//ossid
String certificate = sahPostInfoVo.getCertificate();
if (StringUtils.isNotEmpty(certificate)){
//多个id使用,拆分
String[] split = environment.split(StringUtils.SEPARATOR);
List<String> certificateIdList = Arrays.asList(split);
//单元格处理的核心类
WriteCellData<Void> certificateCellData = new WriteCellData<>();
//图片集合
List<ImageData> imageDataList = new ArrayList<>();
for (String certificateId : certificateIdList) {
//获取图片文件数据逻辑
SysOssVo sysOss = iSysOssService.getById(Long.valueOf(certificateId));
if (ObjectUtil.isNull(sysOss)) {
throw new ServiceException("文件数据不存在!");
}
OssClient client = OssFactory.instance(sysOss.getService());
String ossFileName = sysOss.getFileName();
if (StringUtils.isNotBlank(ossFileName)){
//文件字节数组
byte[] download = client.download(ossFileName);
//图片对象
ImageData imageData = new ImageData();
//存入数据和格式
imageData.setImage(download);
imageData.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG);
//将多个图片收集
imageDataList.add(imageData);
}
}
//放入图片集合数据
certificateCellData.setImageDataList(imageDataList);
//文字设为空(如果想在单元格内也写入文字,可以参考官方文档)
certificateCellData.setType(CellDataTypeEnum.EMPTY);
//写入类的指定字段
sahPostInfoVo.setCertificateBytes(certificateCellData);
}
//导出
ExcelUtil.exportExcel(sahPostInfoVos, "岗位信息", SahPostInfoVoExcel.class, response);
} catch (Exception e) {
throw new RuntimeException("导出Excel异常");
}
}
导出的视图对象
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.zy.sah.hg.domain.SahPostInfo;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import org.dromara.common.core.domain.dto.OssDTO;
import org.dromara.common.translation.annotation.Translation;
import org.dromara.common.translation.constant.TransConstant;
import java.io.Serial;
import java.util.List;
/** 导出视图对象
* @Author: dmz
* @CreateTime: 2024-10-23
*/
@Data
@ExcelIgnoreUnannotated
@AutoMapper(target = SahPostInfo.class)
@ContentRowHeight(60)
@JsonInclude(value = JsonInclude.Include.NON_NULL) public class SahPostInfoVoExcel {
@Serial
private static final long serialVersionUID = 1L;
/**
* 唯一ID
*/
private Long id;
/**
* 岗位名称
*/
@ExcelProperty(value = "岗位名称",index = 1)
private String name;
/**
* 图标
*/
private String icon;
/**
* 介绍
*/
@ExcelProperty(value = "岗位说明详情",index = 3)
private String detail;
/**
* 任职要求
*/
@ExcelProperty(value = "任职要求",index = 5)
private String demand;
/**
* 岗位职责
*/
@ExcelProperty(value = "岗位职责",index = 4)
private String duty;
/**
* 备注
*/
private String remark;
/**
* 岗位说明文件(单张图片)
*/
private Long supportingPaper;
@Translation(type = TransConstant.OSS_ID_TO_URL, mapper = "supportingPaper")
private String supportingPaperUrl;
@ExcelProperty(value = "岗位说明文件",index = 2)
private byte[] supportingPaperInput;
/**
*证书(多张图片)
*/
private String certificate;
private List<OssDTO> certificateDTO;
//如果是一个单元格内多个图片,则使用WriteCellData去收集
@ExcelProperty(value = "相关证书",index = 7)
private WriteCellData<Void> certificateBytes;
}
ImageData(EasyExcel提供的类)
package com.alibaba.excel.metadata.data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
/**
* image
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
public class ImageData extends ClientAnchorData {
/**
* image
*/
private byte[] image;
/**
* image type
*/
private ImageType imageType;
@Getter
public enum ImageType {
/**
* Extended windows meta file
*/
PICTURE_TYPE_EMF(2),
/**
* Windows Meta File
*/
PICTURE_TYPE_WMF(3),
/**
* Mac PICT format
*/
PICTURE_TYPE_PICT(4),
/**
* JPEG format
*/
PICTURE_TYPE_JPEG(5),
/**
* PNG format
*/
PICTURE_TYPE_PNG(6),
/**
* Device independent bitmap
*/
PICTURE_TYPE_DIB(7),
;
int value;
ImageType(int value) {
this.value = value;
}
}
}