作者:GreenCode
出處:https://www.cnblogs.com/nayou/p/13896456.html
正文如下:
# 概述EasyExcel是一個基於Java的簡單、省內存的讀寫Excel的開源項目。在儘可能節約內存的情況下支持讀寫百M的Excel。
github地址:https://github.com/alibaba/easyexcel
# EasyExcel控制表格註解設置 row 高度,不包含表頭標記在 類上@ContentRowHeight(15)@HeadRowHeight(int):
設置 表頭 高度(與 @ContentRowHeight 相反)標記在 類上@HeadRowHeight(20)@ColumnWidth(int):
設置列寬標記在屬性上@ColumnWidth(20)@ExcelProperty(value = String[], index = int):
設置表頭信息value: 表名稱index: 列號
@ExcelProperty(index = 0, value = "學生名字")# SpringBoot實戰應用
導入依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.0.0-beta5</version></dependency>創建Excel實體類對象
@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;}導出Controller
@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);}格式轉換
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;}是不是很easy啊?
長按關注~