前端导出Excel文档

1、下载插件

复制代码
npm i -s  vue-json-excel

2、 引入插件

在 main.js 中

复制代码
import Vue from 'vue'
import JsonExcel from 'vue-json-excel'
 
Vue.component('downloadExcel', JsonExcel)

3、在页面使用

html 复制代码
<template>
    <div>
        <download-excel :data="tableData" :fields="json_fields" :name="excelName">
            <el-button type="primary" plain>导出</el-button>
        </download-excel>
    </div>
</template>

<script>
export default {
  name: "APP",
  data() {
    return {
      tableData: [
        {
            store_id:1,
            product:[]
        }
       ],
     
      excelName: "期初商品库存单.xls", //导出文件名字
      // //导出的列的key和名称表头的设置
      json_fields: {
        仓库: {
          //如果数据需要处理也可以使用回调函数的方式
          field: "store_id",
          callback: (store_id) => {
            return this.storesList.find((e) => e.id == store_id)
              ? this.storesList.find((e) => e.id == store_id).store_name
              : "无";
          },
        },
        商品名称: {
          field: "product",
          callback: (product) => {
            return product[0].product_name;
          },
        },
        条形码: {
          field: "product",
          callback: (product) => {
            return product[0].bar_code;
          },
        },
        规格型号: {
          field: "product",
          callback: (product) => {
            return product[0].spec;
          },
        },
        商品单位: {
          //如果数据需要处理也可以使用回调函数的方式
          field: "product",
          callback: (product) => {
            return this.unitsList.find((e) => e.id == product[0].unit_id)
              ? this.unitsList.find((e) => e.id == product[0].unit_id).unit
              : "无";
          },
        },
        商品数量: "number",
        // 进价: "bid_price",
        // 批发价: "trade_price",
        // 零售价: "retail_price",
        成本价: "price",
        库存预警上限: {
          field: "product",
          callback: (product) => {
            return product[0].superior_limit;
          },
        },
        库存预警下限: {
          //如果数据需要处理也可以使用回调函数的方式
          field: "product",
          callback: (product) => {
            return product[0].lower_limit;
          },
        },
      },
    };
  },
}
</script>

然后导出即可完成

相关推荐
天蓝色的鱼鱼1 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping1 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙2 小时前
[译] Composition in CSS
前端·css
白水清风2 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix2 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278002 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端2 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧2 小时前
Promise 的使用
前端·javascript
NBtab3 小时前
Vite + Vue3项目版本更新检查与页面自动刷新方案
前端
天天扭码3 小时前
来全面地review一下Flex布局(面试可用)
前端·css·面试