添加依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.3</version>
</dependency>
页面
<form action="weiuser/upload" th:action="@{/weiuser/upload}" method="post" enctype="multipart/form-data">
<input style="margin-top: 20px" th:type="file" th:name="file">
<br>
<button id="btn" class="btn btn-success radius" th:type="submit">确认导入</button>
</form>
Controller
@RequestMapping("upload")
public String upload(@RequestParam("file")MultipartFile multipartFile) throws IOException {
Class<WeiUser> clazz=WeiUser.class;
EasyExcel.read(multipartFile.getInputStream(), clazz, new AnalysisEventListener<WeiUser>() {
@Override
public void invoke(WeiUser weiUser, AnalysisContext analysisContext) {
weiUserService.insert(weiUser);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("数据解析完成");
}
}).sheet().doRead();
return "redirect:/weiuser/getUserList";
}