EasyPoi介绍
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法。
快速入门
引入POM
<!-- easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
新增访问接口和实体类
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentEntity implements java.io.Serializable {
/**
* id
*/
private String id;
/**
* 学生姓名
*/
@Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
private String name;
/**
* 学生性别
*/
@Excel(name = "学生性别", replace = {"男_1", "女_2"}, suffix = "生", isImportField = "true_st")
private int sex;
@Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
private Date birthday;
@Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
private Date registrationDate;
}
访问接口
@RestController
@RequestMapping("/student")
public class StudentController {
@GetMapping("/export")
public void export(HttpServletResponse response) {
List<StudentEntity> list = new ArrayList<>();
list.add(new StudentEntity("1", "lili", 2, new Date(), new Date()));
list.add(new StudentEntity("2", "cc", 1, new Date(), new Date()));
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生", "学生"),
StudentEntity.class, list);
try {
// 重置响应对象
String excelName = new String("学生列表".getBytes("UTF-8"), "iso8859-1");
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-disposition", "attachment;filename=" + excelName + ".xlsx");
response.setCharacterEncoding("UTF-8");
// 写出数据输出流到页面
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}