vue中使用xlsx插件导出多sheet excel实现方法

安装xlsx,一定要注意版本:

复制代码
npm i xlsx@0.17.0 -S

package.json:

复制代码
{
  "name": "hello-world",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "element-ui": "^2.15.12",
    "file-saver": "^2.0.5",
    "vue": "^2.6.11",
    "vue-router": "^3.2.0",
    "xlsx": "^0.17.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-plugin-component": "^1.1.1",
    "less": "^3.0.4",
    "less-loader": "^5.0.0",
    "vue-cli-plugin-element": "^1.0.1",
    "vue-template-compiler": "^2.6.11"
  }
}

App.vue:

复制代码
<template>
  <div id="app">
    <el-button type="primary" @click="exportExcel">vue xlsx导出多sheet excel</el-button>
  </div>
</template>

<script>
  import XLSX from "xlsx";
  export default {
    methods: {
      exportExcel() {
        var data1 = [
          ["id", "name", "hot"],
          [1, "C++", 99],
          [2, "JavaScript", 98]
        ];
        var data2 = [
          ["id", "语言", "热度"],
          [1, "C++", 99],
          [2, "JavaScript", 98]
        ];
        const ws1 = XLSX.utils.aoa_to_sheet(data1);
        const ws2 = XLSX.utils.aoa_to_sheet(data2);

        /* generate workbook and add the worksheet */
        const wb = XLSX.utils.book_new();
        // XLSX.utils.book_append_sheet(wb, ws, "test");
        XLSX.utils.book_append_sheet(wb, ws1, "月度统计报表");
        XLSX.utils.book_append_sheet(wb, ws2, "隔离库");

        /* save to file */
        XLSX.writeFile(wb, "test.xlsx");
      }
    }
  };
</script>
相关推荐
信看8 小时前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
爱吃大芒果8 小时前
Flutter 主题与深色模式:全局样式统一与动态切换
开发语言·javascript·flutter·ecmascript·gitcode
king王一帅8 小时前
流式渲染 Incremark、ant-design-x markdown、streammarkdown-vue 全流程方案对比
前端·javascript·人工智能
whyfail10 小时前
Vue原理(暴力版)
前端·vue.js
踢球的打工仔10 小时前
jquery的基本使用(3)
前端·javascript·jquery
徐同保10 小时前
js 点击按钮 把文本转成文件并下载下来
开发语言·javascript·ecmascript
VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue敬老院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
bug总结11 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
程序员爱钓鱼11 小时前
Node.js 编程实战:数据库连接池与性能优化
javascript·后端·node.js