2 Star 4 Fork 1

leiwuhen-67/Export2Excel.js与Blob.js文件

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Export2Excel.js与Blob.js文件

引言:

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

如果想要导出Excel

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

表结构

  • 渲染页面中的表结构是由columns列 和dataList 行,来渲染的 columns为表头数据dataList为表实体内容
column: [
        {
          'key': 'novelId',
          'title': 'ID',
          'align': 'center',
          'width': 150,
          'ellipsis': true,
          render: (h, params) => {
            return h('div', [
              h('span', {
                attrs: {
                  class: 'box',
                  'data-clipboard-text': params.row.novelId
                },
                on: {
                  'click': (data, b) => {
                    var clipboard = new Clipboard('.box')
                  }
                }
              }, params.row.novelId)
            ])
          }
        },
        {
          'key': 'imageUrl',
          'title': '封面',
          'align': 'center',
          'width': 100,
          render: (h, params) => {
            return h('div', [
              h('img', {
                attrs: {
                  src: params.row.imageUrl,
                  alt: '封面'
                },
                style: {
                  'width': '36px',
                  'height': '48px',
                  'verticalAlign': 'center'
                },
                on: {
                  'click': (data, b) => {}
                }
              }, '编辑')
            ])
          }
        },
        {
          'key': 'title',
          'title': '小说名',
          'align': 'center',
          'width': 120
        },
        {
          'key': 'categoryName',
          'title': '二级分类',
          'align': 'center',
          'width': 100
        },
        {
          'key': 'author',
          'title': '作者',
          'align': 'center',
          'width': 150
        },
        {
          'key': 'completeStatus',
          'title': '进度',
          'align': 'center',
          'width': 100,
          render: (h, params) => {
            return h('div', [
              h('span', {}, params.row.completeStatus * 1 === 1 ? '完结' : '未完结')
            ])
          }
        },
        {
          'key': 'updateTime',
          'title': '更新时间',
          'align': 'center',
          'width': 150
        },
        {
          'key': 'status',
          'title': '状态',
          'width': 100,
          'align': 'center',
          render: (h, params) => {
            return h('div', [
              h('span', {}, params.row.status * 1 === 1 ? '有效' : '无效')
            ])
          }
        },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          width: 120,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '8px'
                },
                on: {
                  'click': (data, b) => {
                    global.addTab('编辑小说', 'novelEdit?novelId=' + params.row.novelId + '&parentId=' + this.currentId)
                  }
                }
              }, '编辑'),
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  'click': (data, b) => {
                    const statusMsg = params.row.status === 1 || params.row.status === '1' ? '确认禁用?' : '确认启用?'
                    const _this = this
                    _this.statusModel = true
                    _this.$Modal.confirm({
                      title: '提示',
                      content: `<p>${statusMsg}</p>`,
                      onOk: () => {}
                    })
                  }
                }
              }, params.row.status === 1 || params.row.status === '1' ? '冻结' : '启用')
            ])
          }
        }
      ],

dataList 这里就不写了

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

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

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

    formatJson(filterVal, jsonData) {
    return jsonData.map(v => filterVal.map(j => v[j]))
  },
    export2Excel() {
      const _this = this
   require.ensure([], () => {
     const { export_json_to_excel } = require('../vendor/Export2Excel');
     const tHeader = ['ID', '封面', '小说名','二级分类','作者','进度','更新时间', '状态'];
     const filterVal = ['novelId', 'imageUrl', 'title', 'categoryName', 'author', 'completeStatus', 'updateTime', 'status'];
     const list = _this.dataList;
     const data = _this.formatJson(filterVal, list);
     export_json_to_excel(tHeader, data, '小说列表');
   })
    }

空文件

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/leiwuhen-67/export2exceljs_and_blobjs_files.git
git@gitee.com:leiwuhen-67/export2exceljs_and_blobjs_files.git
leiwuhen-67
export2exceljs_and_blobjs_files
Export2Excel.js与Blob.js文件
master

搜索帮助