帮我看看该如何处理 谢谢
- import React, { PureComponent } from 'react';
- import { connect } from 'dva';
- import {
- Form,
- Input,
- Select,
- Button,
- Card,
- Row,
- Col,
- Radio,
- InputNumber, Upload, Icon,
- } from 'antd';
- import { SpreadSheets, Worksheet, Column } from '@grapecity/spread-sheets-react'; // Excel 插件
- import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css';
- import Excel from '@grapecity/spread-excelio';
- import PageHeaderWrapper from '@/components/PageHeaderWrapper';
- const FormItem = Form.Item;
- const { Option } = Select;
- const { TextArea } = Input;
- let ss = 1;
- const formItemLayoutOne = {
- labelCol: {
- xs: { span: 24 },
- sm: { span: 2 },
- },
- wrapperCol: {
- xs: { span: 24 },
- sm: { span: 21 },
- },
- };
- const formItemLayout = {
- labelCol: {
- xs: { span: 24 },
- sm: { span: 8 },
- },
- wrapperCol: {
- xs: { span: 24 },
- sm: { span: 16 },
- },
- };
- const twoCol = {
- sm: 12,
- xs: 24,
- };
- const threeCol = {
- md: 8,
- sm: 12,
- xs: 24,
- };
- @connect(({ testQuestions, loading }) => ({
- testQuestions,
- submitting: loading.effects['form/submitRegularForm'],
- }))
- @Form.create()
- class AddPracticalExercises extends PureComponent {
- state = {
- sheetList: [], // excel 列表
- };
- /**
- * 创建表单
- * @returns {*}
- */
- getFields() {
- const {
- form: {
- getFieldDecorator,
- getFieldValue,
- }, submitting,
- } = this.props;
- const submitFormLayout = {
- wrapperCol: {
- xs: { span: 24, offset: 0 },
- sm: { span: 10, offset: 7 },
- },
- };
- return (
- <Form onSubmit={this.handleSubmit}>
- <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
- <Col sm={24} style={{ paddingLeft: '4%' }}>
- <FormItem {...formItemLayoutOne} label="上传Excel">
- {getFieldDecorator('upload', {
- rules: [{
- required: true,
- message: '点击选择Excel',
- }],
- valuePropName: 'fileList',
- getValueFromEvent: this.normFile,
- })(
- <Upload name="logo" action="" listType="picture">
- <Button>
- <Icon type="upload"/> 点击选择Excel
- </Button>
- </Upload>,
- )}
- </FormItem>
- </Col>
- </Row>
- <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
- <Col {...threeCol}>
- <FormItem {...formItemLayout} label="账表类型">
- {getFieldDecorator('xxs', {
- rules: [{
- required: true, message: '请选择账表类型',
- }],
- initialValue: '1',
- })(
- <span>
- <Col span={15}>
- <Select placeholder="请选择" style={{ width: '100%' }}>
- <Option value="0">1</Option>
- </Select>
- </Col>
- <Col span={8} push={1}>
- <Button type="primary" icon='plus' onClick={this.addSheet}>添加</Button>
- </Col>
- </span>,
- )}
- </FormItem>
- </Col>
- <Col sm={24} style={{ paddingLeft: '4%' }}> {this.createSheet()} </Col>
- </Row>
- <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
- <Button type="primary" htmlType="submit" loading={submitting}>
- 提交
- </Button>
- </FormItem>
- </Form>
- );
- }
- createSpreadSheets = () => {
- // excel 模拟数据
- const data = [
- { Name: 'Apple', Category: 'Fruit', Price: 1, 'Shopping Place': 'Wal-Mart' },
- { Name: 'Potato', Category: 'Fruit', Price: 2.01, 'Shopping Place': 'Other' },
- { Name: 'Tomato', Category: 'Vegetable', Price: 3.21, 'Shopping Place': 'Other' },
- { Name: 'Sandwich', Category: 'Food', Price: 2, 'Shopping Place': 'Wal-Mart' },
- { Name: 'Hamburger', Category: 'Food', Price: 2, 'Shopping Place': 'Wal-Mart' },
- { Name: 'Grape', Category: 'Fruit', Price: 4, 'Shopping Place': 'Sun Store' },
- ];
- const spreadSheetsConfig = {
- backColor: '#fff',
- showVerticalScrollbar: true,
- showHorizontalScrollbar: true,
- tabStripVisible: true,
- grayAreaBackColor: '#C0C0C0',
- };
- const worksheetConfig = {
- rowCount: 10,
- colCount: 10,
- name: 'sheet1',
- };
- return (
- <SpreadSheets {...spreadSheetsConfig} hostStyle={{ width: '100%', height: '400px' }}>
- <Worksheet {...worksheetConfig} dataSource={data}>
- </Worksheet>
- </SpreadSheets>
- );
- };
- /**
- * 创建Excel 及 表单
- */
- createSheet = () => {
- const { sheetList } = this.state;
- const {
- form: {
- getFieldDecorator,
- },
- } = this.props;
- return sheetList.map((item, i) => {
- return (
- <Card
- key={i}
- type="inner"
- title=""
- style={{ marginBottom: '10px' }}
- extra={<Button type="danger" onClick={() => this.removeSheet(i)}>删除</Button>}
- >
- {this.createSpreadSheets()}
- <FormItem {...formItemLayout} label="数据同步">
- <Button type='primary'>同步</Button>
- </FormItem>
- </Card>
- );
- });
- };
- /**
- * 添加按钮点击
- */
- addSheet = () => {
- const { sheetList } = this.state;
- this.setState({
- sheetList: ['', ...sheetList],
- });
- };
- /**
- * 表单提交
- * @param e
- */
- handleSubmit = e => {
- const { dispatch, form } = this.props;
- e.preventDefault();
- form.validateFieldsAndScroll((err, values) => {
- if (!err) {
- dispatch({
- type: 'form/submitRegularForm',
- payload: values,
- });
- }
- });
- };
- /**
- * 删除Excel
- * @param e
- */
- removeSheet = e => {
- const { sheetList } = this.state;
- const newSheetList = sheetList.map(item => ({ ...item }));
- newSheetList.splice(e, 1);
- this.setState({
- sheetList: newSheetList,
- });
- };
- /**
- * 文件处理
- */
- normFile = e => {
- if (ss === 1) {
- ++ss;
- console.log('Upload event:', e.file.originFileObj);
- new Excel.IO().open(e.file.originFileObj, (s) => {
- console.log( SpreadSheets, '------');
- }, (s) => {
- // console.log(s, '++');
- }, { password: '' });
- if (Array.isArray(e)) {
- return e;
- }
- }
- return e && e.fileList;
- };
- render() {
- return (
- <PageHeaderWrapper title="添加">
- <Card bordered={false}>
- {this.getFields()}
- </Card>
- </PageHeaderWrapper>
- );
- }
- }
- export default AddPracticalExercises;
复制代码
|