SpringBoot项目使用“EasyExcel”导出Excel数据

前有导入,同时就会伴有导出,那么导出来了。

直接进入正题:

第一步:导入EasyExcel的pom
properties标签下引入本人使用的是2.1.6版本鸭~
<easyexcel.version>2.1.6</easyexcel.version>
dependencies标签下引入
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>${easyexcel.version}</version>
</dependency>
到这里,准备工作就做好了,下面进行正式代码环节#

Controller层代码:

public void monitorPointDetailExport(HttpServletResponse response, String sheetName, String fileName, MonitorPointDetailQo monitorPointDetailQo) throws IOException {
    monitorPointService.monitorPointDetailExport(response, sheetName, fileName, monitorPointDetailQo);
}

Service层代码:

void monitorPointDetailExport(HttpServletResponse response, String sheetName, String fileName, MonitorPointDetailQo monitorPointDetailQo) throws IOException;

Impl层代码:

@Override
public void monitorPointDetailExport(HttpServletResponse response, String sheetName, String fileName, MonitorPointDetailQo monitorPointDetailQo) throws IOException {
    // 生成数据
    List<MonitorPointDetailExportVo> respCustomerDailyImports = this.monitorPointDetailNoPage(monitorPointDetailQo);
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    // 这里URLEncoder.encode可以防止中文乱码
    fileName = URLEncoder.encode(fileName, "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
    EasyExcel.write(response.getOutputStream(), MonitorPointDetailExportVo.class)
            .sheet(sheetName)
            .head(this.getMonitorPointDetailHead())
            // 设置字段宽度为自动调整
            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
            .doWrite(respCustomerDailyImports);
}

补充说明:导出的Excel内容的列顺序根据MonitorPointDetailExportVo这个Vo里面的字段决定。

无需多解释了,代码里面有注释,生成数据换成自己的,其他复制。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值