GithubHelp home page GithubHelp logo

export-excel's Introduction

引言:

最近使用vue在做一个后台系统,技术栈 vue + iView,在页面中生成表格后, iView可以实现表格的导出,不过只能导出csv格式的,并不适合项目需求。

如果想要导出Excel

  • 在src目录下创建一个文件(vendor)进入Blob.jsExport2Excel.js
  • npm install -S file-saver 用来生成文件的web应用程序
  • npm install -S xlsx 电子表格格式的解析器
  • npm install -D script-loader 将js挂在在全局下

表结构

  • 渲染页面中的表结构是由columns列 和tableData 行,来渲染的 columns为表头数据tableData为表实体内容
columns1: [
          {
            title: '序号',
            key: 'serNum'
          },
          {
            title: '选择',
            key: 'choose',
            align: 'center',
            render: (h, params) => {
              if (params.row.status !== '1' && params.row.status !== '2') {
                return h('div', [
                  h('checkbox', {
                    props: {
                      type: 'selection'
                    },
                    on: {
                      'input': (val) => {
                        console.log(val)
                      }
                    }
                  })
                ])
              } else {
                return h('span', {
                  style: {
                    color: '#587dde',
                    cursor: 'pointer'
                  },
                  on: {
                    click: () => {
                      // this.$router.push({name: '', query: { id: params.row.id }})
                    }
                  }
                }, '查看订单')
              }
            }
          },
          ...
        ],

tableData 就不写了,具体数据结构查看iViewAPI

在build 目录下webpack.base.conf.js配置 我们要加载时的路径

 alias: {
      'src': path.resolve(__dirname, '../src'),
    }

在当前页面中引入依赖

  require('script-loader!file-saver')
  // 转二进制用
  require('script-loader!src/vendor/Blob')
  // xlsx核心
  require('script-loader!xlsx/dist/xlsx.core.min')

当我们要导出表格执行@click事件调用handleDownload函数

 handleDownload() {
        this.downloadLoading = true
        require.ensure([], () => {
          const {export_json_to_excel} = require('src/vendor/Export2Excel')
          const tHeader = Util.cutValue(this.columns1, 'title')
          const filterVal = Util.cutValue(this.columns1, 'key')
          const list = this.tableData1
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '列表excel')
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      }

Util.cutValue 是公共方法,目的是为了将,tHeader 和filterVal 的值转成数组从而生成表格

 ### Util module
// 截取value值
utils.cutValue = function (target, name) {
  let arr = []
  for (let i = 0; i < target.length; i++) {
    arr.push(target[i][name])
  }
  return arr
}

export-excel's People

Contributors

han6054 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.