easypoi 注解导出Excel实现。

依赖

    <!--新版Excel 导入导出-->

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.3</version>
        </dependency>

实体类加注解

@Excel(name ="终端号",needMerge = true,width = 20,height = 8)
private String terminalCode;//终端号

单个sheet如何实现

/**
 * 功能描述:默认导出方法
 *
 * @param list 导出的实体集合
 * @param fileName 导出的文件名
 * @param pojoClass pojo实体
 * @param exportParams ExportParams封装实体
 * @param response
 * @return
 */
private static void defaultExport(	List<?> list, Class<?> pojoClass, String fileName,
                                      HttpServletResponse response, ExportParams exportParams) {

    Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);

    if (workbook != null) {
        downLoadExcel(fileName, response, workbook);
    }

}

多个sheet如何实现

    List<Map<String, Object>> list = new ArrayList<>();
    
        Map<String, Object> map = new HashMap<>();
        ExportParams exportParams = new ExportParams("title", "sheetName");
        map.put("title", exportParams);
        map.put("entity", OrderReport.class);
        map.put("data", “数据集”);
        list.add(map);
        
/**
 * 功能描述:默认导出方法
 *
 * @param list 导出的实体集合
 * @param fileName 导出的文件名
 * @param response pojo实体
 * @param excelType 一般为ExcelType.HSSF
 * @return
 */
private static void defaultSheetExport(List<Map<String, Object>> list,  String fileName,
                                      HttpServletResponse response, ExcelType excelType) {

    Workbook workbook = ExcelExportUtil.exportExcel(list, excelType);
    if (workbook != null) {
        downLoadExcel(fileName, response, workbook);
    }

}

最终导出

/**
 * 功能描述:Excel导出
 *
 * @param fileName 文件名称
 * @param response
 * @param workbook Excel对象
 * @return
 */
private static void downLoadExcel(	String fileName, HttpServletResponse response,
                                      Workbook workbook) {
    try {
        response.setCharacterEncoding("UTF-8");
        //定义标题以及设置响应头信息
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",URLEncoder.encode(fileName+".xls", "UTF-8"));

        workbook.write(response.getOutputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

vue下载文件

import fileDownload from ‘js-file-download’;

exportReport() {
  this.$http({
    url: ,
    method: 'get',
    responseType: 'arraybuffer'
  }).then(response => {

    let fileName = decodeURI(response.headers['content-disposition']);//-----获取响应头部里面filename的值
    fileDownload(response.data, fileName);  //-----根据fileDownload来下载响应回来的数据
  });
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值