前端导出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>

然后导出即可完成

相关推荐
小飞侠在吗9 小时前
vue toRefs 与 toRef
前端·javascript·vue.js
csuzhucong9 小时前
斜转魔方、斜转扭曲魔方
前端·c++·算法
老华带你飞9 小时前
房屋租赁管理|基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·毕设
燃烧的土豆9 小时前
100¥ 实现的React项目 Keep-Alive 缓存控件
前端·react.js·ai编程
半生过往9 小时前
前端运行PHP 快速上手 使用 PHPStudy Pro 详细搭建与使用指南
开发语言·前端·php
zlpzlpzyd9 小时前
ecmascript中Promise和async/await的区别
开发语言·前端·ecmascript
streaker3039 小时前
从零实现一个“类微信”表情输入组件
前端·vue.js·element
小明记账簿_微信小程序9 小时前
js、node.js获取指定文件下的内容
前端
小明记账簿_微信小程序9 小时前
h5中弹框出现后禁止页面滚动
前端
一个有故事的男同学9 小时前
从零打造专业级前端 SDK (一):架构与工程化
前端·架构