springboot+vue导出excel并下载

  1. 引入easyexcel依赖
xml 复制代码
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>
  1. 编写接口
java 复制代码
/**
     * 批量导出
     *
     * @param ids
     * @return
     */
    @PostMapping("/export")
    public void exportExcel(@RequestBody List<Long> ids, HttpServletResponse response) {
    	// 查询出需要批量导出的数据
        List<Order> orderList = orderService.listByIds(ids);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            EasyExcel.write(response.getOutputStream(), Order.class)
                    .autoCloseStream(true).build()
                    .write(() -> orderList, new WriteSheet()).finish();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
  1. 前端发起导出请求并下载
javascript 复制代码
const exportExcel = async () => {
  // 发送HTTP GET请求获取Excel文件流
  axios({
    url: url地址,
    method: 'POST',
    responseType: 'blob', // 重要
    data: 批量导出的数据id,
    headers: {
      'Authorization': token // 如果需要token,添加到请求头中
    }
  }).then((response) => {
    // 创建一个Blob对象,内容为后端返回的文件流
    const blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});

    // 创建一个a标签,用于触发下载
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = '订单表.xlsx'; // 下载文件的名称
    link.click();

    // 清理资源
    URL.revokeObjectURL(link.href);
  }).catch((error) => {
    console.error('下载失败:', error);
  });
}
相关推荐
瓶子xf11 分钟前
EXCEL-业绩、目标、达成、同比、环比一图呈现
excel
码尚云标签12 分钟前
批量打印Excel条形码
excel·标签打印·条码打印·一维码打印·条码批量打印·标签打印软件·打印教程
神仙别闹2 小时前
基于Vue+Node.js(Express)实现(Web)物联网的蔬菜大棚温湿度监控系统
前端·vue.js·node.js
写代码的比利2 小时前
Spring 调试终于不再痛苦了
spring boot·spring·intellij idea
x吴文龙3 小时前
不再踩坑,在Vue3+vite安装UNOCSS
前端·vue.js
拾光拾趣录3 小时前
让 Vue 动起来!用 Motion for Vue 打造丝滑交互的实战指南
前端·vue.js
泉城老铁3 小时前
在 Element UI 中将 el-radio-group改为纵向排列
前端·vue.js
一只爱撸猫的程序猿4 小时前
构建一个简单的亿级数据迁移方案案例
spring boot·数据分析·ai编程
黑土豆4 小时前
【Vue3 实战】从0到1封装一个“框选截图”组件,顺便聊聊 html2canvas 的那些“坑”
前端·javascript·vue.js
EF@蛐蛐堂4 小时前
Lodash 的终极进化Radashi
前端·javascript·vue.js