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; }