YanTianFeng的知识库

Want Coding

Want Reading

文章 89

访问 18443

评论 2

头像

YanTianFeng

发私信

文章 89
访问 18443
评论 2
Technology and Code
返回顶部

Knowledge  js获取excel内容

标签   excel  

  ( 18 )       ( 0 )


js 获取 excel 内容实现

导入 xlsx

import XLSX from "xlsx";

选择 excel 文件

读取 excel 文件内容

checkSheet() {
  const objFile = document.getElementById('uploadFile')
  if (objFile.value === '') {
    alert('请选择文件!')
    return
  }
  // 获取文件
  const files = objFile.files
  const _that = this

  const fileReader = new FileReader();
  fileReader.onload = function(ev) {
    console.log('ev: ', ev);
    const data = ev.target.result,
          workbook = XLSX.read(data, {
            type: 'binary'
          })
    let dataList = [];

    // 遍历每张表读取
    console.log('workbook: ', workbook);
    for (var sheet in workbook.Sheets) {
      if (workbook.Sheets.hasOwnProperty(sheet) && sheet === 'device') {
        dataList = dataList.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
        console.log('sheet: ', sheet);
      }
    }
    _that.analysisSheet(dataList)
  };

  fileReader.readAsBinaryString(files[0]);
},
analysisSheet(data) {
  console.log('data: ', data);
},