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

然后导出即可完成

相关推荐
wgod8 分钟前
new AbortController()
前端
UXbot14 分钟前
UXbot 是什么?一句指令生成完整应用的 AI 工具
前端·ai·交互·个人开发·ai编程·原型模式·ux
开开心心就好21 分钟前
免费好用:PPT演示计时提醒工具
windows·计算机视觉·计算机外设·逻辑回归·excel·深度优先·csdn开发云
棒棒的唐29 分钟前
WSL2用npm安装的openclaw,无法正常使用openclaw gateway start启动服务的问题
前端·npm·gateway
哔哩哔哩技术38 分钟前
使用Compose Navigation3进行屏幕适配
前端
咬人喵喵2 小时前
E2.COOL 平台深度解析:从特效分类到实战操作指南
前端·编辑器·svg
RisunJan2 小时前
Linux命令-named-checkzone
linux·前端
小陈工2 小时前
Python Web开发入门(十):数据库迁移与版本管理——让数据库变更可控可回滚
前端·数据库·人工智能·python·sql·云原生·架构
吹晚风吧3 小时前
解决vite打包,base配置前缀,nginx的dist包找不到资源
服务器·前端·nginx
weixin199701080163 小时前
《施耐德商品详情页前端性能优化实战》
前端·性能优化