pom依赖如下:
<properties>
<afterturn-easypoi.version>4.2.0</afterturn-easypoi.version>
</properties>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>${afterturn-easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>${afterturn-easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>${afterturn-easypoi.version}</version>
</dependency>
导入代码如下:
@ApiOperation(value = "导入测试")
@PostMapping(value = "/import")
public R<?> importTest( @RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
// 判断文件是否为空
CheckParamExceptionUtil.notNull(file, "E_BD0122");
// 获取文件名
String fileName = file.getOriginalFilename();
// 进一步判断文件是否为空(即判断其大小是否为0或其名称是否为null)
long size = file.getSize();
CheckParamExceptionUtil.isNotBlank(fileName, "E_BD0122");
if (size < 1) {
throw new EmsException("E_BD0122");
}
List<Test> dataList = getImportData(file);
if (CollectionUtils.isEmpty(dataList)) {
throw new EmsException("E_BD0129");
}
}
private List<Test> getImportData(MultipartFile file) throws Exception, IOException {
List<Test> dataList = null;
try (InputStream inputStream = file.getInputStream()) {
ImportParams params = new ImportParams();
// 导入 普通导入
dataList = ExcelImportUtil.importExcel(inputStream, Test.class, params);
} catch (Exception e) {
e.printStackTrace();
throw new EmsException("E_BD0126", e);
}
return dataList;
}
该博客展示了如何在Java应用中使用Afterturn的EasyPoi库进行Excel数据的导入。首先,定义了POM文件中所需的EasyPoi依赖。接着,提供了导入Excel文件的API实现,包括文件有效性检查、读取文件并转换为数据列表,以及处理导入异常。在`getImportData`方法中,使用`ExcelImportUtil`进行Excel内容的解析。
5510

被折叠的 条评论
为什么被折叠?



