赞
踩
在我们做的系统中,经常需要通过excel文件导入添加到我们的数据库,并需要将数据库的列表进行导出保存到本地,那么,SpringBoot应该如何操作呢?
首先EasyExcel官网地址:EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel
前情提要:本例子的前端用的是vue、element-ui
1.导入依赖
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>easyexcel</artifactId>
- <version>3.3.2</version>
- </dependency>
2.将实体类与表进行规范
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @TableName("sys_user")
- public class User {
- @TableId(value = "id",type = IdType.AUTO )
- @ExcelProperty("编号")
- private Integer id;
- @ExcelProperty("用户名")
- private String username;
- @JsonIgnore //不展示密码字段
- private String password;
- @ExcelProperty("昵称")
- private String nickname;
- @ExcelProperty("邮箱")
- private String email;
- @ExcelProperty("电话")
- private String phone;
- @ExcelProperty("地址")
- private String address;
- @ExcelProperty("头像")
- @TableField("avatar_url")
- private String avatarUrl;
- }

ExcelProperty意思相信大家也看得出来,就是将实体类的变量与excel表格的列对应起来。容易理解。
接下来就是写接口了
3.导入
- /**
- * 导入的excel文件对象是MultipartFile类型的
- * @param file
- * @throws IOException
- */
- @PostMapping("/import")
- public void ExcelImport(MultipartFile file)throws IOException{
- InputStream is = file.getInputStream();
- UserReadListener userReadListener = new UserReadListener(userMapper);
- EasyExcel.read(is,User.class,userReadListener)
- .sheet(0)
- .headRowNumber(1)
- .doRead();
-
- }
UserReadListener是一个关键的地方,这里其实就是导入的对象监听器
- import com.alibaba.excel.context.AnalysisContext;
- import com.alibaba.excel.read.listener.ReadListener;
- import com.cbz.springboot.mapper.UserMapper;
-
- public class UserReadListener implements ReadListener<User> {
-
- private UserMapper userMapper;
-
- public UserReadListener(UserMapper userMapper){
- this.userMapper = userMapper;
- }
-
-
- @Override
- public void invoke(User user, AnalysisContext analysisContext) {
- System.out.println("读取到:"+user);
- userMapper.insert(user);
- }
-
- @Override
- public void doAfterAllAnalysed(AnalysisContext analysisContext) {
- System.out.println("读取完毕!!!");
- }
- }

实现ReadListener类的2个方法,invoke就是文件的每一行(除了列名)都会执行一下,所以我们就需要添加到我们的数据库中!doAfterAllAnalysed就是文件的所有内容读取完的时候就会执行一下这个函数。
那么这里为什么不是用Autowired注解来注入UserMapper呢?
答:因为ReadListener不是spring的一个组件,不能进行自动装配!
4.导出
- //导出
- @GetMapping("/export")
- public void ExcelExport(HttpServletResponse response)throws IOException {
- List<User> users = userMapper.selectList(null);
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");//.xlsx格式
- response.setCharacterEncoding("utf-8");
- String fileName = URLEncoder.encode("用户数据", "UTF-8").replaceAll("\\+", "%20");
- response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
-
- EasyExcel.write(response.getOutputStream())
- .head(User.class)
- .excelType(ExcelTypeEnum.XLSX)
- .sheet("用户数据")
- .doWrite(users);
- }
.sheet("用户数据")意思就是如下
URLEncoder.encode("用户数据", "UTF-8")意思就是如下
第一句:
List<User> users = userMapper.selectList(null);
就是把数据库的内容全部查找出来
接下来再写到excel里面,然后再导出!
~~~~~~~~~~~~~~~~~后端到这里结束了,我们看看前端
- <el-upload
- action="http://localhost:9090/user/import" :show-file-list="false" accept=".xlsx"
- :on-success="handleImportSuccess"
- style="display: inline-block;margin-right: 10px;">
- <el-button type="primary">导入 <i class="el-icon-bottom"></i></el-button>
- </el-upload>
- <el-button type="primary" @click="exportExcel">导出 <i class="el-icon-top"></i></el-button>
- </div>
-
- exportExcel(){
- window.open("http://localhost:9090/user/export")
- },
- handleImportSuccess(){
- this.$message.success("导入成功!")
- this.load()
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。