标签:private 数据 void 文件 spring xls att param repos
EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。
github地址:https://github.com/alibaba/easyexcel
开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在帮忙在点个★Star。
设置 row 高度,不包含表头
标记在 类上
@ContentRowHeight(15) //设置行高
设置 表头 高度(与 @ContentRowHeight 相反)
标记在 类上
@HeadRowHeight(20) //设置表头高度
设置列宽
标记在属性上
@ColumnWidth(20) //设置列宽
设置表头信息
value: 表名称
index: 列号
@ExcelProperty(index = 0, value = "学生名字")
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.0-beta5</version>
</dependency>
@Data
@ContentRowHeight(15)
@HeadRowHeight(20)
@ColumnWidth(20)
public class StudentCurrentExcel {
@ExcelProperty(index = 0, value = "学生名字")
private String studentName;
@ExcelProperty(index = 1, value = "性别")
private String gender;
@ExcelProperty(index = 2, value = "年级/班级")
private String deptCode;
@ExcelProperty(index = 3, value = "通道名称")
private String screenName;
@ExcelProperty(index = 4, value = "通行方向")
private String direction;
@ExcelProperty(index = 5, value = "通行时间")
private Date passageTime;
@ExcelProperty(index = 6, value = "体温")
private String temperate;
@ExcelProperty(index = 7, value = "组织结构")
private Integer deptId;
}
@UnAuthorized
@ApiOperation(value = "导出学生通行记录")
@GetMapping("studentCurrents/export")
public void exportStudentCurrents(HttpServletResponse response, HttpServletRequest request) throws IOException {
//设置响应类型
response.setContentType("application/vnd.ms-excel");
//设置字符编码
response.setCharacterEncoding("utf-8");
//设置文件名字
String fileName = "学生通行记录" + DateUtils.getDate("yyyy-MM-dd HH:mm") + ".xlsx";
//设置响应头信息
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
List<StudentCurrent> all = this.studentCurrentService.findAll();
List<StudentCurrentExcel> list = getNotPassData2(all);
//写入文件数据
EasyExcel.write(response.getOutputStream(), StudentCurrentExcel.class).sheet("员工考勤信息").doWrite(list);
}
/**
* 将实体类对象 转为Excel对象
* @param studentCurrentList
* @return
*/
private List<StudentCurrentExcel> getNotPassData2(List<StudentCurrent> studentCurrentList) {
List<StudentCurrentExcel> list = new ArrayList<>();
for (StudentCurrent studentCurrent : studentCurrentList) {
StudentCurrentExcel excel = new StudentCurrentExcel();
Integer gender = studentCurrent.getGender();
excel.setStudentName(studentCurrent.getStudentName());
excel.setGender(studentCurrent.getGender() == 4 ? "男" : "女");
excel.setDeptCode(studentCurrent.getDeptCode());
excel.setDeptId(studentCurrent.getDeptId());
excel.setPassageTime(studentCurrent.getPassageTime());
excel.setTemperate(studentCurrent.getTemperate());
excel.setScreenName(studentCurrent.getScreenName());
list.add(excel);
}
return list;
}
标签:private 数据 void 文件 spring xls att param repos
原文地址:https://www.cnblogs.com/nayou/p/13896456.html