Home > Web Front-end > JS Tutorial > body text

How Vue exports json data to Excel spreadsheet method

小云云
Release: 2017-12-12 13:12:57
Original
2876 people have browsed it

This article mainly introduces the example of Vue exporting json data to Excel spreadsheet. The editor thinks it is quite good. Now I will share it with you and give you a reference. I hope it can help you.

1. Install dependencies (basically the same as before)

npm install file-saver --save
npm install xlsx --save
npm install script-loader --save-dev
Copy after login

2. Download the two required js files Blob.js and Export2Excel. js.

Post the download address here:

Export2Exce_jb51.rar

3. Create a new vendor folder in the src directory and put Blob.js and Export2Excel.js in it .

4. Change the webpack.base.conf.js configuration

In the alias of resolve:

'vendor': path.resolve(__dirname, '../src/vendor')
Copy after login

5. In the .vue file

script part

data(){
 return{
  list:[
    {
     name:'韩版设计时尚风衣大',
     number:'MPM00112',
     salePrice:'¥999.00',
     stocknums:3423,
     salesnums:3423,
     sharenums:3423,
   },
   {
     name:'韩版设计时尚风衣大',
     number:'MPM00112',
     salePrice:'¥999.00',
     stocknums:3423,
     salesnums:3423,
     sharenums:3423,
   },
  ]
 }

methods:{
  formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
  export2Excel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../../vendor/Export2Excel');
        const tHeader = ['商品名称','商品货号','售价','库存','销量','分享',];
        const filterVal = ['name', 'number', 'salePrice', 'stocknums', 'salesnums', 'sharenums', ];
        const list = this.goodsItems;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '商品管理列表');
      })
    }
}
Copy after login

template:

<button @click="export2Excel">导出</button>
Copy after login

Explanation here:

1. The path of require in export2Excel() may need to be adjusted separately due to different personal project structures. If module not found '../../Export2Excel is reported .js' and the like, please modify the path yourself.

2. tHeader is the name of each column and needs to be entered manually.

#3. filterVal is the key value of the list in the data, and it needs to be written by yourself.

#4. Remember to match the list name in the data here

5. Here you can define the exported excel File name

Related recommendations:

PHP Export EXCEL Quick Development Guide

PHP uses native method to export excel instance sharing

jquery export data to excel code example detailed explanation

The above is the detailed content of How Vue exports json data to Excel spreadsheet method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!