vue3中的excel表导出功能(选中导出或导出所有,也可支持vue2)

1.安装模块

npm install xlsx file-saver -S

2.文件导入

import * as XLSX from "xlsx";

import FileSaver from "file-saver"

3.整体代码(可选中导出或导出所有)

<template>
  <div>
    
     <el-button type="warning" @click="down" >文件导出</el-button>

    <el-table  :data="tableData" stripe style="width: 100%">
      <el-table-column prop="date" label="日期" width="180" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="address" label="地址" />
    </el-table>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import * as XLSX from "xlsx";
import FileSaver from "file-saver"
const tableData = [
  {
    date: "2016-05-03",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-02",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-04",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-01",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
];

// 定义文件名
let name=ref("人员统计")
const down=()=>{
   //选中导出时可更改此处数组
    const selectedData = tableData.slice(0,2);
  // 构建导出的表格数据
  const exportData = [
    { date: "日期", name: "姓名", address: "地址" },
    ...selectedData
  ];
    // 注意表格上绑定id,获取dom元素
 const worksheet = XLSX.utils.json_to_sheet(exportData, { skipHeader: true });
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  const workbookOutput = XLSX.write(workbook, { bookType: "xlsx", type: "array" });
    try {
    //  name.value+".xlsx"   name是文件名,后缀拼接一个excel的文件后缀名  
      FileSaver.saveAs(new Blob([workbookOutput], { type: 'application/octet-stream' }), name.value+".xlsx")
    } catch (e) {
      console.log(e) 
    }
}

</script>
<style lang="less"></style>

id绑定表格全部导出

<template>
  <div>
    <el-button type="warning" @click="down">文件导出</el-button>

    <el-table id="table" :data="tableData" stripe style="width: 100%">
      <el-table-column prop="date" label="日期" width="180" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="address" label="地址" />
    </el-table>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import * as XLSX from "xlsx";
import FileSaver from "file-saver";
const tableData = [
  {
    date: "2016-05-03",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-02",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-04",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-01",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
];

// 定义文件名
let name = ref("人员统计");
const down = () => {
  // 注意表格上绑定id,获取dom元素
  var sel = XLSX.utils.table_to_book(document.getElementById("table"), {
    raw: true,
  });
  var selIn = XLSX.write(sel, {
    bookType: "xlsx",
    bookSST: true,
    type: "array",
  });
  try {
    //  name.value+".xlsx"   name是文件名,后缀拼接一个excel的文件后缀名
    FileSaver.saveAs(
      new Blob([selIn], { type: "application/octet-stream" }),
      name.value + ".xlsx"
    );
  } catch (e) {
    console.log(e, selIn);
  }
  return selIn;
};
</script>
<style lang="less"></style>

4.效果如下

相关推荐
Bubluu9 分钟前
浏览器点击视频裁剪当前帧,然后粘贴到页面
开发语言·javascript·音视频
鎈卟誃筅甡21 分钟前
Vuex 的使用和原理详解
前端·javascript
呆呆小雅26 分钟前
二、创建第一个VUE项目
前端·javascript·vue.js
m0_7482393333 分钟前
前端(Ajax)
前端·javascript·ajax
Fighting_p36 分钟前
【记录】列表自动滚动轮播功能实现
前端·javascript·vue.js
前端Hardy38 分钟前
HTML&CSS:超炫丝滑的卡片水波纹效果
前端·javascript·css·3d·html
Domain-zhuo1 小时前
Git和SVN有什么区别?
前端·javascript·vue.js·git·svn·webpack·node.js
一只搬砖的猹2 小时前
cJson系列——常用cJson库函数
linux·前端·javascript·python·物联网·mysql·json
CodeClimb2 小时前
【华为OD-E卷-租车骑绿道 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
"追风者"2 小时前
前端(八)js介绍(1)
前端·javascript