纯前端实现表格导出,支持1000条以上数据

纯前端实现表格导出,支持1000条以上数据


采用vue组件实现,原理大致是: 先通过接口请求到所有的数据,对数据处理之后转化成base64,通过uri数据导出。

项目源码地址

github地址: https://github.com/monoplasty/vue-batch-export

工具函数

// 创建导出数据模板
function CreateTemplate({
  data,
  type,
  customize,
  excelListRows,
  excelTotalAmount,
}) {
  let columns = [];
  let table = null;
  const listArray = [];
  const tableArr = [];
  const {
    list
  } = data;
  switch (type) {
    case 'joiners':
      columns = [{
          title: '序号',
          dataIndex: 'index',
          key: 'index',
        },
        {
          title: '姓名',
          dataIndex: 'name',
          key: 'name',
        },
        {
          title: '手机号',
          dataIndex: 'mobile',
          key: 'mobile',
        },
        {
          title: '当前价格',
          dataIndex: 'current_price',
          key: 'current_price',
        },
        {
          title: '完成时间',
          dataIndex: 'update_time',
          key: 'update_time',
        },
        {
          title: '标记',
          dataIndex: 'is_exchanged',
          key: 'is_exchanged',
        },
        {
          title: '报名时间',
          dataIndex: 'create_time',
          key: 'create_time',
        },
      ];
      break;
    // 每增加一个类型,需要配置表头数据
    case 'groupgoods-joiners':
      columns = [{
          title: '序号',
          dataIndex: 'index',
          key: 'index',
        },
        {
          title: '姓名',
          dataIndex: 'name',
          key: 'name',
        },
        {
          title: '手机号',
          dataIndex: 'mobile',
          key: 'mobile',
        },
        {
          title: '身份',
          dataIndex: 'leader',
          key: 'leader',
        },
        {
          title: '报名时间',
          dataIndex: 'created_at',
          key: 'created_at',
        },
      ];
    default:
      break;
  }
  for (let i = 0; i < excelTotalAmount; i++) {
    listArray.push(list.slice(i * excelListRows, (i + 1) * excelListRows));
  }
  for (let n = 0; n < excelTotalAmount; n++) {
    ((n) => {
      table = document.createElement('table');
      const thead = document.createElement('thead');
      const theadTr = document.createElement('tr');
      const theadTd = columns.map((ele, index) => {
        const th = `<th style='border: 1px solid #000;text-align: center;'>${ele.title}</th>`;
        return th;
      });
      theadTr.innerHTML = theadTd.join('');
      thead.appendChild(theadTr);
      const tbody = document.createElement('tbody');
      const tbodyTrs = (listArray[n] || []).map((ele, index) => {
        const trTds = [];
        columns.forEach((e) => {
          if (e.render) {
            trTds.push(`<td style='border: 1px solid #000;text-align: center;'>${e.render(ele, ((excelListRows * n) + index))}</td>`);
          } else {
            trTds.push(`<td style='border: 1px solid #000;text-align: center;'>${ele[e.dataIndex]}</td>`);
          }
        });
        return `<tr>${trTds.join('')}</tr>`;
      });
      tbody.innerHTML = tbodyTrs.join('');
      table.appendChild(thead);
      table.appendChild(tbody);
      const uri = 'data:application/vnd.ms-excel;base64,';
      const template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">' +
        '<head>' +
        '<!--[if gte mso 9]>' +
        '<xml>' +
        '<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>' +
        '<x:Name>{worksheet}</x:Name>' +
        '<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>' +
        '</x:ExcelWorkbook>' +
        '</xml>' +
        '<![endif]-->' +
        '</xml>' +
        '<meta charset="UTF-8"></head><body><table>{table}</table></body></html>';
      const ctx = {
        worksheet: name || 'Worksheet',
        table: table.innerHTML,
      };
      const excelUrl = uri + base64(format(template, ctx));
      tableArr.push(excelUrl);
    })(n);
  }
  return {
    tableArr,
    excelTotalAmount,
  };
}

使用

直接使用lib目录里的代码作为一个组件,进行引用

import BatchExport from './batch-export.vue'

说明

使用过程总需要调用后台接口获取表格数据,如总数据大于 1000 则会生成多个表格。
欢迎共同讨论,给出建议。 直接留言 或 issue

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值