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

然后导出即可完成

相关推荐
UXbot12 分钟前
UI设计工具推荐合集
前端·人工智能·ui
码路星河13 分钟前
基于 Vue + VueUse 的 WebSocket 优雅封装:打造高可用的全局连接管理方案
javascript·vue.js·websocket
敲敲了个代码37 分钟前
如何优化批量图片上传?队列机制+分片处理+断点续传三连击!(附源码)
前端·javascript·学习·职场和发展·node.js
@AfeiyuO43 分钟前
Vue 引入全局样式scss
前端·vue·scss
光影少年1 小时前
flex布局和grid布局区别,实现两边固定布局中间自适应
前端·css3·web·ai编程
全栈测试笔记1 小时前
异步函数与异步生成器
linux·服务器·前端·数据库·python
EndingCoder2 小时前
配置 tsconfig.json:高级选项
linux·前端·ubuntu·typescript·json
木风小助理2 小时前
JavaStreamAPI的性能审视,优雅语法背后的隐形成本与优化实践
java·前端·数据库
white-persist3 小时前
轻松抓包微信小程序:Proxifier+Burp Suite教程
前端·网络·安全·网络安全·微信小程序·小程序·notepad++
敲敲了个代码4 小时前
多标签页强提醒不重复打扰:从“弹框轰炸”到“共享待处理队列”的实战
java·前端·javascript·面试·架构