Easypoi主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,PDF模板,通过简单的注解和模板语言,完成功能。
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-baseartifactId>
<version>4.1.0version>
dependency>
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-webartifactId>
<version>4.1.0version>
dependency>
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-annotationartifactId>
<version>4.1.0version>
dependency>
/**
* 导出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");
// 导出Excel
outputStream = 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 <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (file == null){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> 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<LzExcelVo> lzExcelVos = new ArrayList<>();//这里可以改为导出数据
String sheetName= "sheet1";
String tl = title+"表";
ExcelUtil.exportExcel(lzExcelVos,tl,sheetName, LzExcelVo.class,fileName,response);
}
/**
* Excel表导入数据
* @param file
* @param qydm
* @return
*/
@Override
public List<String> importLzExcel(MultipartFile file, String qydm) {
//解析excel表数据
List<LzExcelVo> lzExcelVos = ExcelUtil.importExcel(file,1,2,LzExcelVo.class);
//下面就是数据处理的逻辑了
return null;
}
@ApiOperation(value = "导入")
@PostMapping("/import")
public ReturnWrapper<List<String>> importLz(@RequestParam MultipartFile file, String qydm){
return ReturnWrapMapper.ok(iLzService.importLzExcel(file,qydm));
}