一、excel读写测试

1.导入依赖

<properties><easyexcel.version>2.2.0-beta2</easyexcel.version>
</properties><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>${easyexcel.version}</version>
</dependency>

2.创建实体类

@Data
public class User {@ExcelProperty(index = 0,value = "用户编号")private int id;@ExcelProperty(index = 1,value = "用户姓名")private String userName;
}

3.写入excel文件

public class WriteTest {public static void main(String[] args) {List<User> list=new ArrayList<>();for (int i = 0; i < 10; i++) {User user=new User();user.setId(i);user.setUserName("candy"+i);list.add(user);}String path="F:\\git\\ShangGuiGu\\excel\\01.xlsx";EasyExcel.write(path,User.class).sheet("用户信息").doWrite(list);}
}

4.读excel文件

4.1.读之前要添加监听器

public class EventListener extends AnalysisEventListener<User>{/*** 从第二行开始一行一行读取* @param user 实体类* @param analysisContext 内容*/@Overridepublic void invoke(User user, AnalysisContext analysisContext) {System.out.println(user);}/*** 读取第一行* @param headMap* @param context*/@Overridepublic void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {System.out.println("头信息"+headMap);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {}}

4.2读excel文件

public class ReadTest {public static void main(String[] args) {String file="F:\\git\\ShangGuiGu\\excel\\01.xlsx";EasyExcel.read(file,User.class, new EventListener()).sheet().doRead();}
}

二、excel整合SpringBoot

1.编写实体类

@Data
public class DictEeVo {/*** 属性上添加注解,设置表头内容*/@ExcelProperty(value = "id" ,index = 0)private Long id;@ExcelProperty(value = "上级id" ,index = 1)private Long parentId;@ExcelProperty(value = "名称" ,index = 2)private String name;@ExcelProperty(value = "值" ,index = 3)private String value;@ExcelProperty(value = "编码" ,index = 4)private String dictCode;}

2.编写mapper文件

@Repository
public interface DictMapper extends BaseMapper<Dict> {}

3.编写监听器

/*** @FileName: DictListener* @Author Steven* @Date: 2021/3/17*/public class DictListener extends AnalysisEventListener<DictEeVo>{@AutowiredDictMapper dictMapper;public DictListener(DictMapper dictMapper) {this.dictMapper=dictMapper;}@Overridepublic void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {Dict dict=new Dict();BeanUtils.copyProperties(dictEeVo,dict);dictMapper.insert(dict);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {}
}

3.编写service接口

public interface DictService extends IService<Dict>{/*** 导出*  @param response*/void exportData(HttpServletResponse response);/*** @param multipartFile 导入字典*/void importDictData(MultipartFile multipartFile);
}

3.编写service实现类

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {@Overridepublic void exportData(HttpServletResponse response) {try {response.setContentType("application/json");response.setCharacterEncoding("UTF-8");String fileName = URLEncoder.encode("数据字典", "UTF-8");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");List<Dict> dictList = dictMapper.selectList(null);List<DictEeVo> dictVos = new ArrayList<>(dictList.size());for (Dict dict : dictList) {DictEeVo dictVo = new DictEeVo();BeanUtils.copyProperties(dict, dictVo);dictVos.add(dictVo);}EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictVos);} catch (Exception e) {throw new YyghException("数据导出失败!!!", 123);}}/*** 调用后清空所有方法** @param multipartFile 导入字典*/@CacheEvict(value = "dict", allEntries = true)@Overridepublic void importDictData(MultipartFile multipartFile) {try {EasyExcel.read(multipartFile.getInputStream(), DictEeVo.class, new DictListener(baseMapper)).sheet().doRead();} catch (IOException e) {e.printStackTrace();}}
}

4.编写controller

@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {@ApiOperation("数据导出")@GetMapping("/exportData")public Result exportData(HttpServletResponse response){dictService.exportData(response);return Result.ok();}@ApiOperation("导入字典数据")@PostMapping("/importData")public Result importData(MultipartFile file){dictService.importDictData(file);return Result.ok();}
}

SpringBoot整合easyexcel实现导入导出相关推荐

  1. springboot整合poi-tl根据模板导出word

    springboot整合poi-tl根据模板导出word poi-tl中文文档:http://deepoove.com/poi-tl/ 引入所需包 <dependency><grou ...

  2. 使用EasyExcel实现导入导出功能

    使用EasyExcel实现导入导出功能 一.导出 1.使用ideal新建一个maven项目,并在pom.xml文件中引入EasyExcel依赖 <!--easyexcel实现导入导出--> ...

  3. java使用EasyExcel实现导入导出几种方式(导入、模板导出、和不需要模板的导出)

    java通过EasyExcel实现导入导出(导入.模板导出.和不需要模板的导出) 此文章只是涉及到简单的导入导出 通过实体模板导入数据 无实体模板导入数据 导出数据 通过模板导出数据 使用到的mave ...

  4. SpringBoot 整合EasyExcel详解(一)-高性能Excel方案

    SpringBoot 整合EasyExcel详解(二)-写Excel SpringBoot 整合EasyExcel详解(三)-填充Excel-官方原版 一.概述 Java解析.生成Excel比较有名的 ...

  5. EasyExcel(二) 导入导出excel的数据格式转换

    EasyExcel(二) 导入导出excel的数据格式转换 关于easyExcel的基本用法我就不在多说了,有需要的可以自己点击该链接去学习基本的使用,主要对这里面经常用到的一些监听器和拦截器讲一下, ...

  6. SpringBoot整合EasyExcel

    文章目录 SpringBoot整合EasyExcel 1.EasyExcel简介 2.使用EasyExcel实现写 2.1 创建实体类 2.2 测试写Excel 3.使用EasyExcel实现读 3. ...

  7. Springboot整合poi +vue实现导出导入Excle表格数据展示图形

    工具: idea 数据库: mysql 框架:Springboot 准备工作: 1.导入主要依赖 (poi) <dependency><groupId>org.apache.p ...

  8. EasyExcel 模板导入导出

    一.环境 1.开发工具:idea2018.1 2.jar 管理:maven 3.3.3 3.项目类型:springboot 二.pom.xml <?xml version="1.0&q ...

  9. SpringBoot 项目实现 Excel 导入导出功能

    背景 Excel 导入与导出是项目中经常用到的功能,在 Java 中常用 poi 实现 Excel 的导入与导出.由于 poi 占用内存较大,在高并发下很容易发生 OOM 或者频繁 fullgc,阿里 ...

最新文章

  1. MinGW 与MSVC的区别
  2. 数据下载工作笔记三:脚本
  3. nacos 本地测试_Nacos集群配置实例(windows下测试)
  4. java 异常 日志_java中的异常、断言、日志(一)
  5. linux 圣经软件,Ubuntu(Linux)下好用的中文圣经
  6. 三星中文AI助手Bixby发布,现在,这是“一家AI商用技术公司”
  7. 十、Python-模块
  8. 机器学习之监督学习(二)——神经网络
  9. zoj 3229 Shoot the Bullet(无源汇上下界最大流)
  10. 【POJ 3279】【开关问题】Fliptile【暑期 No.5】
  11. 9008刷机工具_黔隆科技刷机教程OPPOR11T忘记密码免刷机保资料解屏幕锁教程
  12. 华为是怎样研发的(12)——FMEA分析
  13. dlink网卡驱动 linux,D-Link友讯
  14. qt 飞扬青云_细数Qt开发的各种坑(欢迎围观)
  15. Android 集成百度地图服务和驾车导航jar包冲突、驾车导航引入armeabi-v7a平台
  16. Scala中的fold和reduce理解
  17. invalid vcs root mapping 怎么解决_一加黑鲨华硕OPPO等手机root后微信指纹支付不可用怎么解决...
  18. 什么原因让你对程序员失去了往日的热情?
  19. 电脑C盘爆满了怎么办
  20. SQL 中INSERT INTO 的使用方法

热门文章

  1. JavaScript | 继承
  2. SlidingMenu的简单使用
  3. 修改可选项文件实现自动连接数据库服务器
  4. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)...
  5. 转载:浏览器开发系列第一篇:如何获取最新chromium源码
  6. Ado.net 创建DataTable
  7. WebService开发
  8. html 分级切换菜单_FL studio系列教程(十六):FL Studio查看菜单讲解
  9. php调用函数的变量,从内PHP函数调用的变量在外部函数使用
  10. html偷拍代码,一段植入木马的html代码