EasyPoi——导出导入表格数据工具
创始人
2024-02-07 20:43:46
0

文章目录

  • 文档
  • 概念
  • 环境搭建(maven)
  • 工具类
  • 主要使用注解
    • @Excel 主要用的注解
  • 实战
    • 实体类(部分)
    • 导出
      • Controller层
      • 导出结果
    • 导入
      • service层
      • controller层


文档

官方文档

概念

Easypoi主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,PDF模板,通过简单的注解和模板语言,完成功能。

环境搭建(maven)


cn.afterturneasypoi-base4.1.0


cn.afterturneasypoi-web4.1.0


cn.afterturneasypoi-annotation4.1.0

工具类


/*** 导出Excel*/public static void exportExcel(List list, String title, String sheetName, Class entity, String fileName, HttpServletResponse response) {ExportParams exportParams = new ExportParams(title, sheetName);//冻结表头exportParams.setCreateHeadRows(true);Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entity, list);if (workbook == null) {throw new RuntimeException("Excel表导出失败");}OutputStream outputStream = null;BufferedOutputStream buffOutputStream = null;try {// 指定下载的文件名--设置响应头response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls");//response.setContentType("application/vnd.ms-excel;charset=UTF-8");response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);response.setCharacterEncoding("UTF-8");// 导出ExceloutputStream = response.getOutputStream();buffOutputStream = new BufferedOutputStream(outputStream);workbook.write(buffOutputStream);buffOutputStream.flush();} catch (Exception e) {e.printStackTrace();} finally {try {if (outputStream != null) {outputStream.close();}if (buffOutputStream != null) {buffOutputStream.close();}if (workbook != null) {workbook.close();}} catch (Exception e) {e.printStackTrace();}}}/*** 导入* @param file 需要导入的文件* @param titleRows 标题占几行* @param headerRows 头部占几行* @param pojoClass 转化为对应的实体类* @return 返回解析后的实体类对象集合*/public static  List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass){if (file == null){return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);List list = null;try {list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);}catch (NoSuchElementException e){e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return list;}

主要使用注解

作用于实体类
@Excel 作用到filed上面,是对Excel一列的一个描述
@ExcelCollection 表示一个集合,主要针对一对多的导出,比如一个老师对应多个科目,科目就可以用集合表示

@Excel 主要用的注解

  • name —— 导出时表格内的描述
  • orderNum ——导出时所在的列
  • groupName ——导出时合并单元格的父级描述
  • fixedIndex——导入时强制获取某一列的数据(如果没有,导入数据时获取合并单元格的数据会有问题,我找了半天)
    想看具体的注解就看一下官方文档。

实战

实体类(部分)

在这里插入图片描述

导出

Controller层

    @ApiOperation(value = "下载模板")@GetMapping("/exportLz")public void exportLzExcel(HttpServletResponse response, String fileName, String title){List lzExcelVos = new ArrayList<>();//这里可以改为导出数据String sheetName= "sheet1";String tl = title+"表";ExcelUtil.exportExcel(lzExcelVos,tl,sheetName, LzExcelVo.class,fileName,response);}

导出结果

在这里插入图片描述

导入

service层

    /*** Excel表导入数据* @param file* @param qydm* @return*/@Overridepublic List importLzExcel(MultipartFile file, String qydm) {//解析excel表数据List lzExcelVos = ExcelUtil.importExcel(file,1,2,LzExcelVo.class);//下面就是数据处理的逻辑了return null;}

controller层

    @ApiOperation(value = "导入")@PostMapping("/import")public ReturnWrapper> importLz(@RequestParam MultipartFile file, String qydm){return ReturnWrapMapper.ok(iLzService.importLzExcel(file,qydm));}

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...