excel 导出

前段页面


var msg;  
                 var loading = this.$loading({
                     lock: true,
                     text: msg,
                     spinner: 'el-icon-loading',
                     background: 'rgba(0, 0, 0, 0.7)'
                 });
                 axios({
                     url: basePath,
                     method: "post",
                     data: _this.queryParam,
                     responseType: "arraybuffer",
                 }).then(function (res) {
                     loading.close();
                     var blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'});
                     var fileNameSplit = res.headers['content-disposition'].split('=');
                     var fileName = decodeURI(fileNameSplit[fileNameSplit.length - 1])
                     if ('download' in document.createElement('a')) { // 非IE 下载
                         var link = document.createElement('a');
                         link.href = window.URL.createObjectURL(blob);
                         link.download = fileName;
                         link.style.display = 'none';
                         document.body.appendChild(link);
                         link.click();
                         URL.revokeObjectURL(link.href);
                         document.body.removeChild(link);
                     } else { // IE10+下载
                         navigator.msSaveOrOpenBlob(blob, fileName)
                     }

                 }).catch(function (reason) {
                     loading.close();
                     messageTip.error(whiteListTag, "导出操作失败...");
                 });

                 return false;

后台逻辑
在方法中获取参数:
@RequestBody Map<String,Object> param

设置响应

response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String fileName = URLEncoder.encode("导出文件名称_" + date, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

ExcelWriter excelWriter = null;

        try {
            // 头的策略(背景设置为绿色)
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
            WriteFont headWriteFont = new WriteFont();
            // 字体大小
            headWriteFont.setFontHeightInPoints((short)10);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 内容的策略(这里用的是默认)
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            WriteFont contentWriteFont = new WriteFont();
            // 字体大小
            contentWriteFont.setFontHeightInPoints((short)10);
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
            // 这里 指定文件
            excelWriter = EasyExcel.write(response.getOutputStream(), MyBean.class).registerWriteHandler(horizontalCellStyleStrategy).build();
            autoWhiteListService.exportMethod(excelWriter,param);
        } catch (Exception e) {
            logger.error("导出Excel失败", e);
        } finally {
            // 关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
public void exportMethod(ExcelWriter excelWriter , Map<String, Object> queryParam) {

        // 起始时间
        logger.info("exportFeenoticeeWL 导出Excel开始Start" );
        long start = System.currentTimeMillis();

        // 查询信息
        List<MyBean> MyBeanList= 查询结果;

        WriteSheet writeSheet = EasyExcel.writerSheet(1, "Sheet1").build();
        excelWriter.write(MyBeanList, writeSheet);

        long exportTime = System.currentTimeMillis()-start;
        logger.info("导出Excel总耗时: {}ms", exportTime);
    }
@Getter
@Setter
public class MyBean{

    @ColumnWidth(20)
    @ExcelProperty(value = "姓名", index = 0)
    private String name;


    @ColumnWidth(20)
    @ExcelProperty(value = "性别", index = 1)
    private String sex;

    @ExcelIgnore
    private String engName;

    @ExcelProperty(value = "状态", index = 2)
    private String state;
}
<dependency>
   <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.6</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值