js 下载url返回的excel数据,并解析为json

XLSX GitHub地址:https://github.com/SheetJS/sheetjs/blob/github/dist/xlsx.full.min.js

需要先引入:XLSX.full.min.js

javascript 复制代码
// 下载文件的请求
fetch(downloadFileUrl).then(response => {
  return rsp.blob()
}).then(data => {
  let reader = new FileReader()
  // reader.readAsDataURL(blob)
  reader.readAsArrayBuffer(blob)
  reader.onload = (e) => {
    console.log('e:都数据流返回的对象:', e)
    let filedata = e.target.result // 文件数据
    // excel都数据流解析数据
    let workbook = XLSX.read(btoa(fixdata(filedata)), {type: 'base64'})
    console.log('读文件后:', workbook)
    // 需要解析第几个Sheet页?
  	let sheet = workbook.Sheets[workbook.SheetNames[0]]
    // sheet解析为json array
    let jsonArray = XLSX.utils.sheet_to_json(sheet)
    console.log('excel解析后的数据:', jsonArray)

    // do domething 解析后的数据jsonArray如何处理
  }
})


// 文件流转 base64
function fixdata(data) {
  var o = '',
    l = 0,
    w = 10240;
  for (; l < data.byteLength / w; ++l)
    o += String.fromCharCode.apply(
      null,
      new Uint8Array(data.slice(l * w, l * w + w))
    );
  o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
  return o;
}

参考链接:
使用XLSX来解析Excel的内容 - 掘金

相关推荐
wtsolutions3 小时前
Using the JSON to Excel API - Programmatic Access for Developers
json·excel
Miketutu3 小时前
Flutter学习 - 组件通信与网络请求Dio
开发语言·前端·javascript
摘星编程3 小时前
React Native for OpenHarmony 实战:Swiper 滑动组件详解
javascript·react native·react.js
qq_435139573 小时前
EasyExcel(FastExcel)Excel导出功能 技术文档
excel
鸣弦artha3 小时前
Flutter框架跨平台鸿蒙开发——Build流程深度解析
开发语言·javascript·flutter
LongJ_Sir5 小时前
Cesium--可拖拽气泡弹窗(Vue3版)
javascript
跟着珅聪学java6 小时前
JavaScript 中定义全局变量的教程
javascript
wtsolutions6 小时前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions6 小时前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel
午安~婉6 小时前
整理知识点
前端·javascript·vue