官方文档
Easypoi主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,PDF模板,通过简单的注解和模板语言,完成功能。
cn.afterturn easypoi-base 4.1.0
cn.afterturn easypoi-web 4.1.0
cn.afterturn easypoi-annotation 4.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 表示一个集合,主要针对一对多的导出,比如一个老师对应多个科目,科目就可以用集合表示
@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);}
/*** 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;}
@ApiOperation(value = "导入")@PostMapping("/import")public ReturnWrapper> importLz(@RequestParam MultipartFile file, String qydm){return ReturnWrapMapper.ok(iLzService.importLzExcel(file,qydm));}