将数据库中查询的结果导出为Excel

引用:

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;

代码:

    @Override
    public void exportChangeList(AnalysisVO analysis, HttpServletResponse response) {
        List<AnalysisVO> records = this.changeList(new Page<>(1, -1), analysis).getRecords();
        Assert.notEmpty(records, () -> new ServiceException("无导出数据"));
        ExportParams exportParams = new ExportParams("变更列表(单位:个)", "变更列表", ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, AnalysisExcel.class, BeanUtil.copy(records, AnalysisExcel.class));
        try (OutputStream out = response.getOutputStream()) {
            response.setHeader("Content-Disposition", "attachment; filename=changeList.xlsx");
            response.setContentType("application/vnd.ms-excel");
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实体类:

import cn.afterturn.easypoi.excel.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class AnalysisExcel {
    @Excel(name = "序号", format = "isAddIndex")
    private Integer index;
    @ApiModelProperty(value = "主管单位ID")
    private String budgetManageUnitId;
    @Excel(name = "主管单位名称", width = 60)
    private String budgetManageUnitName;
    @Excel(name = "新建类", width = 25)
    private String newClass;
    @Excel(name = "运维类", width = 25)
    private String operationsClass;
    @Excel(name = "服务类", width = 25)
    private String serviceClass;
    @Excel(name = "排名", width = 30)
    private String rank;
}
  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值