> 文章列表 > excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

文章目录

  • 前言
  • 1 EasyExcel的导入导出
    • 导出
      • 1 导入依赖
      • 2 项目中的CourseEntity实体类
      • 3 CoureseVo VO类 (对CourseEntity进行EasyExcel导入导出操作)
      • 4 导出代码的编写 并最终测试导出效果
      • 5 (前端内容 可选)通过vue按钮点击 导出
    • 导入
      • 1 导入依赖 跟导出相同
      • 2 创建回调监听器
      • 3 编写导入代码
      • 4 最终测试

前言

介绍
excel的导入导出
两种方式
(1 EasyExcel(alibaba) 2 Hutool工具类)

已经实现了 1 EasyExcel 的导入导出
待完善 2023/4/18


1 EasyExcel的导入导出

导出

1 导入依赖

<!--      easyexcel  --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.1</version></dependency>

2 项目中的CourseEntity实体类

项目用到的课程实体类  这个实体类是我们DAO层绑定用的不能使用这个实体类操作EasyExcel 我们等会儿再创建一个VO类
用VO类 操作EasyExcel这里使用的是mybatis-plus的操作方式@TableName("course")  注解绑定的是数据库中的course表@TableId(type = IdType.AUTO)  主键ID ID为AUTO自增策略

excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)
excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)


/*** 课程表*/
@Data
@TableName("course")
public class CourseEntity {/*** 主键*/@TableId(type = IdType.AUTO)private Integer id;/*** 课程id*/private Integer cid;/*** 课程名*/private String name;
}

3 CoureseVo VO类 (对CourseEntity进行EasyExcel导入导出操作)

@ExcelIgnore 忽略 也就是导出的时候没有这个字段@ExcelProperty(value = "课程id",index = 0)
value是指 导出到excel中的列名称
Index 是指在第几列 0 代表在第一列

excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

@Data
public class CoureseVo {/*** 主键*/@ExcelIgnoreprivate Integer id;/*** 课程id*/@ExcelProperty(value = "课程id",index = 0)private Integer cid;/*** 课程名*/@ExcelProperty(value = "课程名称",index = 1)private String name;
}

4 导出代码的编写 并最终测试导出效果

这里为了方便 
就在controller层写了
你当然可以抽出逻辑写到service层 这都无所谓
@RestController
@RequestMapping("/excel")
public class EasyExcelController {//注入的是Mybatis-plus Dao层// 这个CourseDao 与数据库中的course 表已经绑定了//这里注入 DAO 直接调Dao的方法进行测试@AutowiredCourseDao courseDao;@RequestMapping("/export")public void export(HttpServletResponse response) throws IOException {//1 设置下载response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode("数据字典", "UTF-8");response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");// 2 查所有course实体  封装到coureseVo中  再把coureseVo通过Easyexcel写出//   coureseVo相当于 course实体 和 Easyexcel工具的一个中介  实现了解耦List<CourseEntity> courseEntities = courseDao.selectList(null);ArrayList<CoureseVo> vos = new ArrayList<>();for (CourseEntity courseEntity : courseEntities) {CoureseVo coureseVo = new CoureseVo();BeanUtils.copyProperties(courseEntity,coureseVo);vos.add(coureseVo);}//write写出(输出位置,输出类型) .sheet(标题) .diwrite(实际写出的list集合数据)EasyExcel.write(response.getOutputStream(), CoureseVo.class).sheet("数据库课程").doWrite(vos);}}

结果 测试

直接启动项目,浏览器 url 端口加路径访问
结果
excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

5 (前端内容 可选)通过vue按钮点击 导出

1 <template 标签内容><div class="el-toolbar">
<div class="el-toolbar-body"style="justify-content: flex-start;">
<el-button type="text"@click="exportData"><i class="fa fa-plus"/> 导出</el-button>
</div>
</div>2 <script标签内容>
调我们后端写的接口exportData() {
window.location.href = 'http://localhost:8202/admin/cmn/dict/exportData'
}

导入

1 导入依赖 跟导出相同

2 创建回调监听器

package com.example.cat.EasyExcelListen;import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.cat.dao.CourseDao;
import com.example.cat.entity.CoureseVo;
import com.example.cat.entity.CourseEntity;
import org.springframework.beans.BeanUtils;//监听器类
//需要继承阿里的 AnalysisEventListener 泛型里写 EasyExcel工具和实体类的  中介VO类
public class courseListener extends AnalysisEventListener<CoureseVo> {//这里注入 mybatis-plus Dao层 方便我们对从excel表中读到的数据进行处理private CourseDao courseDao;public courseListener(CourseDao courseDao) {this.courseDao = courseDao;}//这个方法会 一行一行的读取 excel//把excel 中的一行内容变成 一个 中介类coureseVo@Overridepublic void invoke(CoureseVo coureseVo, AnalysisContext analysisContext) {//创建数据库中对应的实体类CourseEntity courseEntity = new CourseEntity();//把每个中介类VO的 属性 copy给 数据库中对应的实体类BeanUtils.copyProperties(coureseVo,courseEntity);//添加到数据库中 实现到导入courseDao.insert(courseEntity);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {}
}

3 编写导入代码

我这里直接在cont

这里为了方便 
就在controller层写了
你当然可以抽出逻辑写到service层 这都无所谓
@RestController
@RequestMapping("/excel")
public class EasyExcelController {//注入的是Mybatis-plus Dao层// 这个CourseDao 与数据库中的course 表已经绑定了//这里注入 DAO 直接调Dao的方法进行测试@AutowiredCourseDao courseDao;@RequestMapping("/import")public void importData(MultipartFile file) throws IOException {System.out.println("打印出来的是_" + file.toString());//导入操作//  read(文件中,读的类型,设置监听器(传入一个Mybatis-plus的dao 方便数据添加))EasyExcel.read(file.getInputStream(),CoureseVo.class,new courseListener(courseDao)).sheet().doRead();}}

4 最终测试

先准备一个excel
excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)

使用POSTMAN访问接口
excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)
插入成功
excel的导入导出的两种方案 (1 EasyExcel 2 Hutool工具类)