导出Excel

v1.4.1
Parker 5 years ago
parent d2f081758c
commit b54a4e4a86

@ -18,6 +18,7 @@ package org.opsli.core.base.concroller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
@ -90,6 +91,10 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
/** Model 泛型游标 */
private static final int MODEL_INDEX = 1;
/** Excel 最大导出数量 防止OOM */
@Value("${opsli.export-excel-max-count:20000}")
private Integer exportExcelMaxCount;
@Autowired(required = false)
protected S IService;
@Autowired
@ -211,7 +216,7 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), timerCount);
msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), DateUtil.formatBetween(timerCount));
// 导出成功
resultVo = ResultVo.success(msgInfo);
resultVo.setCode(CoreMsg.EXCEL_IMPORT_SUCCESS.getCode());
@ -219,7 +224,8 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), timerCount, e.getMessage());
msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), DateUtil.formatBetween(timerCount),
e.getMessage());
// 导入失败
resultVo = ResultVo.error(CoreMsg.EXCEL_IMPORT_ERROR.getCode(), msgInfo);
}
@ -274,6 +280,14 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
try {
List<E> modelList = Lists.newArrayList();
if(queryWrapper != null){
// 获得数量 大于 阈值 禁止导出, 防止OOM
int count = IService.count(queryWrapper);
if(count > exportExcelMaxCount){
String maxError = StrUtil.format(CoreMsg.EXCEL_EXPORT_MAX.getMessage(), count, exportExcelMaxCount);
// 超出最大导出数量
throw new ExcelPluginException(CoreMsg.EXCEL_EXPORT_MAX.getCode(), maxError);
}
List<T> entityList = IService.findList(queryWrapper);
// 转化类型
modelList = WrapperUtil.transformInstance(entityList, modelClazz);
@ -283,7 +297,8 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage(), modelList.size(), timerCount);
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage(), modelList.size(),
DateUtil.formatBetween(timerCount));
// 导出成功
resultVo = ResultVo.success(msgInfo);
resultVo.setCode(CoreMsg.EXCEL_EXPORT_SUCCESS.getCode());
@ -291,7 +306,7 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), timerCount, e.getMessage());
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), DateUtil.formatBetween(timerCount), e.getMessage());
// 导出失败
resultVo = ResultVo.error(CoreMsg.EXCEL_EXPORT_ERROR.getCode(), msgInfo);
}

@ -42,12 +42,14 @@ public enum CoreMsg implements BaseMsg {
/**
* Excel
*/
EXCEL_EXPORT_SUCCESS(200,"Excel 导出成功! - 数据行数:{} - 耗时:{}毫秒"),
EXCEL_EXPORT_ERROR(10301,"Excel 导出失败! - 耗时:{}毫秒 - 失败信息:{}"),
EXCEL_IMPORT_SUCCESS(200,"EXCEL 导入成功! - 耗时:{}毫秒"),
EXCEL_IMPORT_ERROR(10303,"Excel导入失败! - 耗时:{}毫秒 - 失败信息:{}"),
EXCEL_EXPORT_SUCCESS(200,"Excel 导出成功! - 数据行数:{} - 耗时:{}"),
EXCEL_EXPORT_ERROR(10301,"Excel 导出失败! - 耗时:{} - 失败信息:{}"),
EXCEL_IMPORT_SUCCESS(200,"EXCEL 导入成功! - 耗时:{}"),
EXCEL_IMPORT_ERROR(10303,"Excel导入失败! - 耗时:{} - 失败信息:{}"),
EXCEL_IMPORT_NO(10304,""),
EXCEL_FILE_NULL(10305,"请选择文件"),
EXCEL_EXPORT_MAX(10700, "超出最大导出数量, 当前数据[{}]条,允许最大阈值[{}]条"),
/**
*

@ -195,3 +195,6 @@ opsli:
# 失败锁定时间(秒)
slip-lock-speed: 300
# Excel 最大导出数量 防止OOM
export-excel-max-count: 20000

Loading…
Cancel
Save