VUE导出数据为excel,xlsx

53 篇文章 0 订阅
40 篇文章 0 订阅

npm需要安装插件:

npm install --save file-saver xlsx

代码如下:

<template>

  <a-table :columns="columns" :data-source="data" id="out-table">

    <a slot="name" slot-scope="text">{{ text }}</a>

    <span slot="customTitle"><a-icon type="smile-o" /> Name</span>

    <span slot="tags" slot-scope="tags">

      <a-tag

        v-for="tag in tags"

        :key="tag"

        :color="

          tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'

        "

      >

        {{ tag.toUpperCase() }}

      </a-tag>

    </span>

    <span slot="action" slot-scope="text, record">

      <a>Invite 一 {{ record.name }}</a>

      <a-divider type="vertical" />

      <a>Delete</a>

      <a-divider type="vertical" />

      <a class="ant-dropdown-link"> More actions <a-icon type="down" /> </a>

    </span>

  </a-table>

</template>

<script>

import FileSaver from "file-saver";

import XLSX from "xlsx";

const columns = [

  {

    dataIndex: "name",

    key: "name",

    slots: { title: "customTitle" },

    scopedSlots: { customRender: "name" },

  },

  {

    title: "Age",

    dataIndex: "age",

    key: "age",

  },

  {

    title: "Address",

    dataIndex: "address",

    key: "address",

  },

  {

    title: "Tags",

    key: "tags",

    dataIndex: "tags",

    scopedSlots: { customRender: "tags" },

  },

  {

    title: "Action",

    key: "action",

    scopedSlots: { customRender: "action" },

  },

];

const data = [

  {

    key: "1",

    name: "John Brown",

    age: 32,

    address: "New York No. 1 Lake Park",

    tags: ["nice", "developer"],

  },

  {

    key: "2",

    name: "Jim Green",

    age: 42,

    address: "London No. 1 Lake Park",

    tags: ["loser"],

  },

  {

    key: "3",

    name: "Joe Black",

    age: 32,

    address: "Sidney No. 1 Lake Park",

    tags: ["cool", "teacher"],

  },

];

export default {

  name: "login",

  data() {

    return {

      data,

      columns,

    };

  },

  methods: {

    onChange() {},

    //da导出表格

    exportExcel() {

      /* generate workbook object from table */

      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));

      /* get binary string as output */

      var wbout = XLSX.write(wb, {

        bookType: "xlsx",

        bookSST: true,

        type: "array",

      });

      try {

        FileSaver.saveAs(

          new Blob([wbout], { type: "application/octet-stream" }),

          "订单详情.xlsx"

        );

      } catch (e) {

        if (typeof console !== "undefined") console.log(e, wbout);

      }

      return wbout;

    },

    // 导入表格

  },

  mounted() {

    this.exportExcel();

  },

};

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>

</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qaakd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值