vue项目导出excel实现

1、springboot后端

注:后端导出excel使用的是easypoi插件实现
1、实体类

package com.springbootapi.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;

public class EasyPoiExport {

    @Excel(name = "名字", width = 30)
    private String name;

    @Excel(name = "年龄", width = 30)
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2、控制层

@PostMapping("/excelExport")
    public void excelExport(@RequestBody Map<String, String> person, HttpServletResponse response) throws Exception {
        List<EasyPoiExport> result = new ArrayList<>();
        EasyPoiExport easyPoiExport = new EasyPoiExport();
        easyPoiExport.setName(person.get("name"));
        easyPoiExport.setAge(22);
        result.add(easyPoiExport);
        result.add(easyPoiExport);
        String title = "导出excel";
        try {
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(title, "UTF-8"));
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导出", "sheet"), EasyPoiExport.class, result);
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2、vue前端

  1. api
import request from './index'

function excelExport(name) {
    let data = {
        name
    }
    const result = request({
        url: '/easyPoiExport/excelExport',
        method: 'post',
        responseType: 'blob',
        data
    })
    return result
}

export default {
    excelExport
}

2、dowload方法

export function download(data,fileName){
    if(!data){
        return
    }
    let url = window.URL.createObjectURL(new Blob([data],{type:'application/vnd.ms-excel'}))
    let link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', fileName)

    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}

3、vue页面调用api获取后台数据,调用dowload方法导出excel

<template>
  <div class=''>
    <el-button @click="excelExport">导出</el-button>
  </div>
</template>

<script>
import API from '@/api/requestMethods'
import { download } from '@/utils'
export default {
  name: 'easypoiExport',
  data() {
    return {

    }
  },
  methods: {
    excelExport() {
      API.excelExport('test').then(res => {
        let fileName = decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1])
        download(res.data, fileName)
      }).catch(e => {
        this.$message.error(`error:${e.message}`)
      })
    }
  }
}
</script>

注:mock模块会影响原生的axios请求,使得服务器返回的blob类型变成乱码,导出的数据变为乱码。如果项目使用了mock需关闭mock。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值