Bootstrap

el-tabe 配合 xlsx 导出 excel 文件

一、安装依赖

pnpm add element-plus
pnpm add xlsx

二、在 main.js 引入

import { createApp } from "vue";

import ElementPlus from "element-plus";

import "element-plus/dist/index.css";

import App from "./App.vue";



const app = createApp(App);

app.use(ElementPlus);

app.mount("#app");

三、使用 xlsx 导出

<template>

  <div>

    <el-button type="primary" @click="exportExcel">导出Excel</el-button>

    <el-table :data="tableData" border style="width: 100%">

      <el-table-column prop="name" label="姓名" width="180"></el-table-column>

      <el-table-column prop="age" label="年龄" width="180"></el-table-column>

      <el-table-column prop="gender" label="性别" width="180"></el-table-column>

    </el-table>

  </div>

</template>

<script setup>

  import { ref } from "vue";

  import XLSX from "xlsx";

  // 模拟表格数据,实际中可以是从接口获取等来源的数据

  const tableData = ref([

    { name: "张三", age: 25, gender: "男" },

    { name: "李四", age: 30, gender: "女" },

    { name: "王五", age: 28, gender: "男" },

  ]);

  const exportExcel = () => {

    const worksheet = XLSX.utils.json_to_sheet(tableData.value);

    const workbook = XLSX.utils.book_new();

    XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");

    XLSX.writeFile(workbook, "人员信息表.xlsx");

  };

</script>

<style scoped></style>

;