在上一节的基础上,本文演示下如何导出excel数据。
Excel导出操作演示
- 继承ocean-easyexcel SDK
<dependency>
<groupId>com.angel.ocean</groupId>
<artifactId>ocean-easyexcel</artifactId>
<version>1.0.0</version>
</dependency>
- 定义Excel数据导出模型
package com.angel.ocean.domain.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.BorderStyleEnum;
import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import lombok.Data;
/**
* Excel导出数据结构样例
*/
@HeadStyle(fillPatternType = FillPatternTypeEnum.DEFAULT, horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderTop = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN, borderLeft = BorderStyleEnum.THIN)
@HeadRowHeight(value = 20)
@HeadFontStyle(fontHeightInPoints = 11, bold = BooleanEnum.TRUE)
@ContentRowHeight(value = 20)
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderTop = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN, borderLeft = BorderStyleEnum.THIN)
@Data
public class UserDataExportVO {
@ExcelProperty("用户名")
@ColumnWidth(20)
private String username;
@ExcelProperty("密码")
@ColumnWidth(20)
private String password;
@ExcelProperty("注册时间")
@ColumnWidth(30)
private String createTime;
}
- 定义Excel导入接口
/**
* 导出excel
* @param response
*/
@GetMapping("export")
public void exportExcel(HttpServletResponse response) {
List<UserDataExportVO> exportData = new ArrayList<>();
for(int i = 0; i < 16; i++) {
UserDataExportVO userDataExportVO = new UserDataExportVO();
userDataExportVO.setUsername("admin");
userDataExportVO.setPassword("123456");
userDataExportVO.setCreateTime("2022-04-24 10:56");
exportData.add(userDataExportVO);
}
EasyExcelUtil.writeExcel(response,System.currentTimeMillis() + ".xlsx" ,"EXCEL导出数据", exportData, UserDataExportVO.class);
}