导出代码:
ExcelWriterSheetBuilder builder = EasyExcel.write(response.getOutputStream())
// 默认自动关闭流,出现异常时会返回一个有部分数据的Excel。这里设置不关闭流,由catch处理,返回错误json
.autoCloseStream(Boolean.FALSE)
// 单一表
.sheet(fileName)
// 表头
.head(excelHeader)
// 写策略:自动列宽
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
// 注册自定义写策略
if (!CollectionUtils.isEmpty(writeHandlers)) {
for (WriteHandler writeHandler : writeHandlers) {
builder.registerWriteHandler(writeHandler);
}
}
// 写自定义内容
builder.doWrite(excelContent);
excelContent:fillVehicleFile就是图片
int size = data.size();
if (size == 0) {
return Collections.emptyList();
}
// 所有行
List<List<Object>> rows = new ArrayList<>(size);
// 一行的所有列
List<Object> columns;
EnterVehicleInfoBo enterVehicleInfoBo;
String fileBaseUrl = Global.getProperty("userfiles.basedir");
File file;
SysFile sysFile;
Object fillVehicleFile = "加载失败";
for (int i = 0; i < size; i++) {
enterVehicleInfoBo = data.get(i);
columns = new ArrayList<>(10);
columns.add(String.valueOf(i + 1));
columns.add(enterVehicleInfoBo.getVehicleNum());
columns.add(enterVehicleInfoBo.getVehicleName());
columns.add(enterVehicleInfoBo.getCarOwnerName());
columns.add(enterVehicleInfoBo.getCarOwnerPhone());
columns.add(enterVehicleInfoBo.getCurrentWeight().toPlainString());
columns.add(enterVehicleInfoBo.getOriginalWeight().toPlainString());
// 填充行驶证图片
sysFile = enterVehicleInfoBo.getVehicleFile();
if (sysFile != null) {
file = new File(fileBaseUrl + sysFile.getFilePath());
if (file.exists()) {
fillVehicleFile = file;
}
}
columns.add(fillVehicleFile);
columns.add(enterVehicleInfoBo.getCreateBy());
columns.add(DateUtil.format(enterVehicleInfoBo.getCreateDate()));
rows.add(columns);
}
return rows;
Microsoft Office显示效果:
WPS Office显示效果:
Microsoft Office图片另存后显示效果:纯黑