js导出Excel

import XLSX from 'xlsx'

import XLSX_SAVE from 'file-saver'

// xlsx 文件输出操作方法
function s2ab(s) {
  const buf = new ArrayBuffer(s.length);
  const view = new Uint8Array(buf);
  for (let i = 0; i !== s.length; ++i) {
    view[i] = s.charCodeAt(i) & 0xFF;
  }
  return buf;
}
// 导出方法
exportExcel() {
  this.loading2 = true
  fetchOrderList({ ...this.searchform, page: 1, pageSize: this.pagination.total })
    .then(res=>{
      if (res.body.status == 'success') {
        let item = res.body.page.items
        let children = [['订单列表'],['用户openid','订单编号','商品名称','支付金额','支付积分','购买数量','订单时间','订单状态','订单类型']]
        if (item && item.length) {
          item.forEach(com => {
            let childrenText =  [com.openId, com.orderProductId, com.prodName, com.payPrice, com.payPoint, com.productNum,com.addTime,com.orderStatus,com.pointType]
            children.push(childrenText)
          })
        }
        /**
         * 数据导出格式为
         * [
         *   ['cols1','cols2','cols3'],
         *   ['data1','data2','data3']
         * ]
         */

          // covert json to sheet
        const ws = XLSX.utils.aoa_to_sheet(children);
        const wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "订单列表");
        // save
        const wbout = XLSX.write(wb, {type: "binary", bookType: "xlsx"})
        XLSX_SAVE.saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), "订单列表.xlsx");
      } else {
        this.$message.error(res.body.message);
      }

      this.loading2=false
    })


}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbsyi/article/details/80321045
文章标签: js导出 js Excel
个人分类: js导出 js Excel
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭