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>
相关推荐
江城开朗的豌豆2 分钟前
Vue组件data必须用函数?这个设计暗藏玄机!
前端·javascript·vue.js
前端小巷子9 分钟前
web域名解析
前端·javascript·面试
江城开朗的豌豆17 分钟前
组件封装实战:如何设计灵活又好用的前端组件?
前端·javascript·vue.js
The_era_achievs_hero1 小时前
微信131~140
开发语言·javascript·微信
良木林2 小时前
JavaScript书写基础和基本数据类型
开发语言·前端·javascript
工业甲酰苯胺8 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
谢尔登12 小时前
【React Native】ScrollView 和 FlatList 组件
javascript·react native·react.js
然我13 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
kk_stoper13 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源
晨枫阳13 小时前
前端VUE项目-day1
前端·javascript·vue.js