其他分享
首页 > 其他分享> > excel

excel

作者:互联网

import React from 'react';
import { Button, Form, Input, Table, Upload } from 'antd';
import 'antd/dist/antd.css';
import XLSX from 'xlsx'

class MyTestForm extends React.Component {
  constructor(props) {
    super(props)
    this.index = 1
    this.state = {
      dataSource:[]
    }
  }

  handleChange = (info) => {
    // console.log('info',info)
    if (info.file.status !== 'uploading') {
      console.log('---', info.file, info.fileList);
      this.readWorkbookFromLocalFile(info.file.originFileObj)
    }
  }

  readWorkbookFromLocalFile = (file, callback) => {
    var reader = new FileReader();
    reader.onload = function (e) {
      var data = e.target.result;
      // 读取二进制的excel
      var workbook = XLSX.read(data, { type: 'binary' });
      console.log('workbook',workbook.Sheets.sheet)
      let excel = workbook.Sheets.sheet
      let dataObj = {} 
      let i = 0
      Object.keys(excel).forEach((key) => {
        if(key.startsWith('B')) {
          i++
          dataObj['currencyCode_'+key.slice(1)] = excel[key].w
        } else if(key.startsWith('C')) {
          dataObj['anencyCode_'+key.slice(1)] = excel[key].w
        } else if(key.startsWith('D')) {
          dataObj['doumaCode_'+key.slice(1)] = excel[key].w
        } else if(key.startsWith('E')) {
          dataObj['cccccCode_'+key.slice(1)] = excel[key].w
        } else if(key.startsWith('F')) {
          dataObj['bbbbCode_'+key.slice(1)] = excel[key].w
        } else if(key.startsWith('G')) {
          dataObj['xxxxCode_'+key.slice(1)] = excel[key].w
        }
      })
      console.log('dataObj',dataObj)
      if (callback) callback(workbook);
    };
    reader.readAsBinaryString(file);
  }

  success = () => {
    
  }

  handleChange2 = (e) => {
    console.log('e', e.target.files)
    this.readWorkbookFromLocalFile(e.target.files[0])
  }

  render() {
    return (
      <div>
        <Upload onChange={this.handleChange}><Button>上传</Button></Upload>
        <input type="file" id="excel-file" onChange={this.handleChange2} />
      </div>

    )
  }
}

export default Form.create({ name: 'sourceDialog' })(MyTestForm);

标签:info,startsWith,slice,dataObj,excel,key
来源: https://www.cnblogs.com/hyhwy/p/15021978.html