mirror of https://gitee.com/y_project/RuoYi.git
Pre Merge pull request !562 from jianxiong/N/A
commit
94c15b7825
|
@ -300,6 +300,37 @@ public class ExcelUtil<T>
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据注解模板类,将上传的文件解析为实体类集合
|
||||
*
|
||||
* @param file 上传的 Excel 文件
|
||||
* @param t 实体类类型
|
||||
* @return 解析后的数据集,若异常则返回空列表
|
||||
*/
|
||||
public <T> List<T> analyzeExcelToList(MultipartFile file, Class<T> t) {
|
||||
if (file == null || file.isEmpty()) {
|
||||
log.warn("上传的文件为空");
|
||||
return List.of(); // 返回空列表而非 null
|
||||
}
|
||||
|
||||
if (t == null) {
|
||||
log.error("目标实体类类型不能为空");
|
||||
throw new UtilException("目标实体类类型不能为空");
|
||||
}
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
ExcelUtil<T> excelUtil = new ExcelUtil<>(t);
|
||||
return excelUtil.importExcel(inputStream); // 直接使用流式处理
|
||||
} catch (Exception e) {
|
||||
log.error("解析Excel文件到目标实体类异常", e);
|
||||
throw new UtilException("解析Excel文件到目标实体类失败");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 对excel表单默认第一个索引名转换成list
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue