1.导入 easyexcel 依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.11</version>
</dependency>
2. 定义导出模型
@Data
public class AlarmStatisticsVO implements Serializable {
private static final long serialVersionUID = -8537331825851278563L;
@ExcelProperty(value = "应用区", order = 2)
@ColumnWidth(15)
private String appZone;
@ExcelProperty(value = "应用名称", order = 1)
@ColumnWidth(20)
private String appCode;
@ExcelIgnore
private String alarmType;
@ExcelProperty(value = "告警类型", order = 3)
@ColumnWidth(15)
private String alarmTypeName;
@ExcelIgnore
private String bizCode;
@ExcelProperty(value = "告警数量", order = 5)
@ColumnWidth(20)
private Long alarmCount;
@ExcelProperty(value = "环比增幅", order = 6)
@ColumnWidth(20)
private String increasePercent;
@ExcelProperty(value = "上周期告警数", order = 6)
@ColumnWidth(25)
private Long prevAlarmCount;
}
3. 导出逻辑
public void exportExcel(HttpServletResponse response, AlarmStatisticsQuery query) throws IOException {
List<AlarmStatisticsVO> items = qureryData();//todo
// 设置文本内省
response.setContentType("application/vnd.ms-excel");
// 设置字符编码
response.setCharacterEncoding("utf-8");
// 设置响应头
String fileName = URLEncoder.encode("告警统计列表", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), AlarmStatisticsVO.class).sheet("告警统计列表").doWrite(items);
}
4. 暴露Controler http 接口,浏览器调用接口可直接下载文件