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);
上一篇:字符串的常用方法&判断一个数是不是正整数


下一篇:css