首页 > 其他 > 详细

excel

时间:2021-07-17 10:48:04      阅读:32      评论:0      收藏:0      [点我收藏+]
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);

excel

原文:https://www.cnblogs.com/hyhwy/p/15021978.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!