有的时候,导出的excel文件中会带有图片,可能是项目内置的,可能是在本地服务器存放的,也可能是在云服务器上,如何在导出Excel时兼容这几种情况呢?
实体类
package com.minghe.jiaozhu.model.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.converters.string.StringImageConverter;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.BorderStyleEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import lombok.Data;
@Data
@HeadRowHeight(30)//设置表头的行高度
@ContentRowHeight(60)//设置单元格的高度
@ColumnWidth(100 / 8)
//属性注释在最下
@ContentStyle(wrapped = BooleanEnum.TRUE, verticalAlignment = VerticalAlignmentEnum.CENTER, borderBottom = BorderStyleEnum.THIN, borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN)
public class ExportHeadersValueImageExcel {
//cellWritervalue对应ValueCellWriteHandler里的cellWritervalue
/**
* 金
*/
@ExcelProperty(value = {"七灵图","总纲","五行", "金"})
private String jin;
/**
* 木
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}","五行", "木"})
private String mu;
/**
* 水
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}","五行", "水"})
private String shui;
/**
* 火
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}","五行", "火"})
private String huo;
/**
* 土
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}","五行", "土"})
private String tu;
/**
* 阴
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}","两仪", "阴"})
private String yin;
/**
* 阳
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}","两仪", "阳"})
private String yang;
/**
* 如果图片用string类型 必须指定转换器,string默认转换成string
*/
@ExcelProperty(value = {"七灵图","${cellWriterValue}", "两仪","灵图"},converter = StringImageConverter.class)
@ColumnWidth(30)//图片的列宽单独设置
private String imagePath;
//有五种写入图片的方式,url是写图片的地址.测试中发现InputStream转换有问题 实际应用中可以看看具体什么问题
/* private File file;
private InputStream inputStream;
private String string;
private byte[] byteArray;
private URL url;*/
}
代码
public void exportHeadersValueImage(String path) {
String sheetName = "sheetName";
String imagePath = "D:\\test\\miao.jpg";
List<ExportHeadersValueImageExcel> excels = new ArrayList<>();
ExportHeadersValueImageExcel imageExcel = new ExportHeadersValueImageExcel();
imageExcel.setJin("10");
imageExcel.setMu("2.00");
imageExcel.setShui("3.00");
imageExcel.setHuo("4.00");
imageExcel.setTu("5.00");
imageExcel.setYin("6.00");
imageExcel.setYang("7.01");
imageExcel.setImagePath(imagePath);
excels.add(imageExcel);
EasyExcel.write(path, ExportHeadersValueImageExcel.class)
.sheet(sheetName).doWrite(excels);
}
效果
实体类中,字段上可以加注解@ColumnWidth(30)来单独设置该字段的列宽,@ExcelProperty注解上要指定converter @ExcelProperty(value = {"七灵图", "两仪","灵图"},converter = StringImageConverter.class)
图片的方式有五种,文件,文件流,string(我理解为path),字节流,url(适用于图片在云端存储的)
/* private File file;
private InputStream inputStream;
private String string;
private byte[] byteArray;
private URL url;*/
git仓库:导入导出: 导入导出的实例
更多导出场景:多场景easyExcel导出excel文件(一)-优快云博客
读取excel场景:多场景easyExcel读取excel文件(二)-优快云博客
根据模板填充excel:多场景easyExcel根据模板填充excel文件(三)-优快云博客