找回密码
 立即注册

QQ登录

只需一步,快速开始

lzynfd

注册会员

2

主题

8

帖子

34

积分

注册会员

积分
34
最新发帖
lzynfd
注册会员   /  发表于:2019-1-11 16:33  /   查看:6647  /  回复:12
本帖最后由 lzynfd 于 2019-1-11 17:13 编辑

如何在react 中使用导入导出功能

我目前的项目 用的是 ant design pro
关于react 文档几乎没有

12 个回复

倒序浏览
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-1-15 16:39:18
推荐
您好,很抱歉我们的文档还不完善,导致这个问题延误了很久,目前已经找到解决办法:

  1. class SpreadSheetsCon extends Component {

  2.     constructor(props) {
  3.         super(props);
  4.         this.state = {
  5.             newTabVisible:true,
  6.             tabStripVisible:true,
  7.             showHorizontalScrollbar: true,
  8.             showVerticalScrollbar:true,
  9.             allowUserZoom : true,
  10.             allowUserResize : true,
  11.             spreadBackColor: '#FFFFFF',
  12.             grayAreaBackColor: '#E4E4E4',
  13.         };
  14.         this.hostStyle = {
  15.             top: '90px',
  16.             bottom: '130px'
  17.         };
  18.         this.autoGenerateColumns = false;
  19.         this.data = dataService.getPersonAddressData();
  20.                 this.workbookInitialized = function(workbook){
  21.                         window.spread = workbook;
  22.                 }
  23.                 //this.findControl = GC.Spread.Sheets.findControl;
  24.     }
复制代码


您注意workbookInitialized 这个接口,是SpreadJS提供的一个加载完毕事件,

可以用来获取页面上SpreadJS 实例。

  1.         importExcel(){
  2.                 var excelFile = document.getElementById("fileDemo").files[0];
  3.                 var password = "";
  4.                 // here is excel IO API
  5.                  new Excel.IO().open(excelFile, function (json) {
  6.                        
  7.                         var workbookObj = json;
  8.                         var spread = window.spread;
  9.                         spread.fromJSON(workbookObj);
  10.                 }, function (e) {
  11.                         // process error
  12.                         alert(e.errorMessage);
  13.                 }, {password: password});
  14.         }
复制代码

  1. <SpreadSheets workbookInitialized={this.workbookInitialized}
  2.                                                                   newTabVisible={this.state.newTabVisible} tabStripVisible={this.state.tabStripVisible}
  3.                                   showHorizontalScrollbar={this.state.showHorizontalScrollbar}
  4.                                   showVerticalScrollbar={this.state.showVerticalScrollbar}
  5.                                   backColor={this.state.spreadBackColor} grayAreaBackColor={this.state.grayAreaBackColor}
  6.                                   allowUserZoom={this.state.allowUserZoom} allowUserResize={this.state.allowUserResize}>
  7.                         <Worksheet dataSource={this.data}
  8.                                    autoGenerateColumns={this.autoGenerateColumns}>
  9.                             <Column width={150} dataField="Name"/>
  10.                             <Column width={150} dataField="CountryRegionCode"/>
  11.                             <Column width={100} dataField="City"/>
  12.                             <Column width={200} dataField="AddressLine"/>
  13.                             <Column width={100} dataField="PostalCode"/>
  14.                         </Worksheet>
  15.                     </SpreadSheets>
复制代码

评分

参与人数 1满意度 +5 收起 理由
lzynfd + 5

查看全部评分

回复 使用道具 举报
AlexZ讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-1-11 18:29:45
推荐
可以参考下面的思路:

使用两个 Spread,其中一个隐藏,隐藏的 Spread 对象导入后,通过 sheet 的 fromJSON 和 toJSON方法讲你需要的 sheet 转移到另一个 Spread 中


请参考下面的博客文章:https://www.grapecity.com.cn/blogs/sjs-single-sheet-loading
回复 使用道具 举报
AlexZ讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2019-1-11 17:51:31
沙发
请参考下面的代码

  1. import  '@grapecity/spread-sheets-vue'
  2.         import * as Excel from "@grapecity/spread-excelio"
  3.         import GC from '@grapecity/spread-sheets'
  4.         import * as FileSaver from "file-saver"
  5.         /*
  6.         GC.Spread.Sheets.LicenseKey = Excel.LicenseKey = "yourkey";
  7.         */
  8.         export default {
  9.                 //name: 'sample-header'
  10.                 data(){
  11.                   return {
  12.                         dataSource: [
  13.                            { name: 'Alice', age: 27, birthday: '1985/08/31', position: 'PM' }
  14.                         ]
  15.                   }
  16.                 },
  17.                 methods:{
  18.                         spreadInitHandle: function (spread) {
  19.                                 window.mySpread = spread;
  20.                                 //GC.Spread.Common.CultureManager.culture('zh-cn');
  21.                                 GC.Spread.Common.CultureManager.culture('ja-jp');
  22.                                 //GC.Spread.Common.CultureManager.culture('en-us');
  23.                                 let sheet = spread.getActiveSheet();
  24.                                 let fullDateTimePattern = 'dddd, MMMM dd, yyyy h:mm:ss tt';
  25.                                 let date = new Date();
  26.                                 sheet.setValue(13, 3, date);
  27.                                 sheet.getCell(13, 3).formatter(fullDateTimePattern);
  28.                                 //在这里通过spread对象调用对应方法获取对应数据,如getValue,getDataSource,getArray等
  29.                                 //这里通过setRowCount,setColumnCount方法来设置显示的行数和列数
  30.                                 sheet.setRowCount(50)
  31.                                 sheet.setColumnCount(15);
  32.                                
  33.             },
  34.                         exportExcel(e){
  35.                                 this.excelIO = new Excel.IO();
  36.                                 //let spread = GC.Spread.Sheet
  37.                                 if (window.mySpread) {
  38.                                         let ssjson = window.mySpread.toJSON();
  39.                                         this.excelIO.save(ssjson,(blob) => {
  40.                                                 FileSaver.saveAs(blob, "test.xlsx");
  41.                                         }, (error) =>{
  42.                                                 console.log(error);
  43.                                         });
  44.                                 }
  45.                         }

  46.                 }
  47.         }
  48.        
  49. </script>
复制代码


回复 使用道具 举报
lzynfd
注册会员   /  发表于:2019-1-11 18:21:16
板凳
AlexZ 发表于 2019-1-11 17:51
请参考下面的代码

在 react 中怎么把导入的excel 表格 显示在最初创建的表格上???
回复 使用道具 举报
lzynfd
注册会员   /  发表于:2019-1-14 09:19:17
5#
AlexZ 发表于 2019-1-11 18:29
可以参考下面的思路:

使用两个 Spread,其中一个隐藏,隐藏的 Spread 对象导入后,通过 sheet 的 fromJ ...

帮我看看该如何处理  谢谢
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'dva';
  3. import {
  4.   Form,
  5.   Input,
  6.   Select,
  7.   Button,
  8.   Card,
  9.   Row,
  10.   Col,
  11.   Radio,
  12.   InputNumber, Upload, Icon,
  13. } from 'antd';
  14. import { SpreadSheets, Worksheet, Column } from '@grapecity/spread-sheets-react'; // Excel 插件
  15. import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css';
  16. import Excel from '@grapecity/spread-excelio';
  17. import PageHeaderWrapper from '@/components/PageHeaderWrapper';

  18. const FormItem = Form.Item;
  19. const { Option } = Select;
  20. const { TextArea } = Input;
  21. let ss = 1;

  22. const formItemLayoutOne = {
  23.   labelCol: {
  24.     xs: { span: 24 },
  25.     sm: { span: 2 },
  26.   },
  27.   wrapperCol: {
  28.     xs: { span: 24 },
  29.     sm: { span: 21 },
  30.   },
  31. };
  32. const formItemLayout = {
  33.   labelCol: {
  34.     xs: { span: 24 },
  35.     sm: { span: 8 },
  36.   },
  37.   wrapperCol: {
  38.     xs: { span: 24 },
  39.     sm: { span: 16 },
  40.   },
  41. };
  42. const twoCol = {
  43.   sm: 12,
  44.   xs: 24,
  45. };
  46. const threeCol = {
  47.   md: 8,
  48.   sm: 12,
  49.   xs: 24,
  50. };

  51. @connect(({ testQuestions, loading }) => ({
  52.   testQuestions,
  53.   submitting: loading.effects['form/submitRegularForm'],
  54. }))
  55. @Form.create()
  56. class AddPracticalExercises extends PureComponent {
  57.   state = {
  58.     sheetList: [],  // excel 列表
  59.   };


  60.   /**
  61.    * 创建表单
  62.    * @returns {*}
  63.    */
  64.   getFields() {
  65.     const {
  66.       form: {
  67.         getFieldDecorator,
  68.         getFieldValue,
  69.       }, submitting,
  70.     } = this.props;

  71.     const submitFormLayout = {
  72.       wrapperCol: {
  73.         xs: { span: 24, offset: 0 },
  74.         sm: { span: 10, offset: 7 },
  75.       },
  76.     };
  77.     return (
  78.       <Form onSubmit={this.handleSubmit}>
  79.         <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
  80.           <Col sm={24} style={{ paddingLeft: '4%' }}>
  81.             <FormItem {...formItemLayoutOne} label="上传Excel">
  82.               {getFieldDecorator('upload', {
  83.                 rules: [{
  84.                   required: true,
  85.                   message: '点击选择Excel',
  86.                 }],
  87.                 valuePropName: 'fileList',
  88.                 getValueFromEvent: this.normFile,
  89.               })(
  90.                 <Upload name="logo" action="" listType="picture">
  91.                   <Button>
  92.                     <Icon type="upload"/> 点击选择Excel
  93.                   </Button>
  94.                 </Upload>,
  95.               )}
  96.             </FormItem>
  97.           </Col>
  98.         </Row>
  99.         <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
  100.           <Col {...threeCol}>
  101.             <FormItem {...formItemLayout} label="账表类型">
  102.               {getFieldDecorator('xxs', {
  103.                 rules: [{
  104.                   required: true, message: '请选择账表类型',
  105.                 }],
  106.                 initialValue: '1',
  107.               })(
  108.                 <span>
  109.                   <Col span={15}>
  110.                     <Select placeholder="请选择" style={{ width: '100%' }}>
  111.                       <Option value="0">1</Option>
  112.                     </Select>
  113.                   </Col>
  114.                   <Col span={8} push={1}>
  115.                     <Button type="primary" icon='plus' onClick={this.addSheet}>添加</Button>
  116.                   </Col>
  117.                 </span>,
  118.               )}
  119.             </FormItem>
  120.           </Col>
  121.           <Col sm={24} style={{ paddingLeft: '4%' }}> {this.createSheet()} </Col>
  122.         </Row>
  123.         <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
  124.           <Button type="primary" htmlType="submit" loading={submitting}>
  125.             提交
  126.           </Button>
  127.         </FormItem>
  128.       </Form>
  129.     );
  130.   }

  131.   createSpreadSheets = () => {
  132.     // excel 模拟数据
  133.     const data = [
  134.       { Name: 'Apple', Category: 'Fruit', Price: 1, 'Shopping Place': 'Wal-Mart' },
  135.       { Name: 'Potato', Category: 'Fruit', Price: 2.01, 'Shopping Place': 'Other' },
  136.       { Name: 'Tomato', Category: 'Vegetable', Price: 3.21, 'Shopping Place': 'Other' },
  137.       { Name: 'Sandwich', Category: 'Food', Price: 2, 'Shopping Place': 'Wal-Mart' },
  138.       { Name: 'Hamburger', Category: 'Food', Price: 2, 'Shopping Place': 'Wal-Mart' },
  139.       { Name: 'Grape', Category: 'Fruit', Price: 4, 'Shopping Place': 'Sun Store' },
  140.     ];

  141.     const spreadSheetsConfig = {
  142.       backColor: '#fff',
  143.       showVerticalScrollbar: true,
  144.       showHorizontalScrollbar: true,
  145.       tabStripVisible: true,
  146.       grayAreaBackColor: '#C0C0C0',

  147.     };
  148.     const worksheetConfig = {
  149.       rowCount: 10,
  150.       colCount: 10,
  151.       name: 'sheet1',
  152.     };

  153.     return (
  154.       <SpreadSheets {...spreadSheetsConfig} hostStyle={{ width: '100%', height: '400px' }}>
  155.         <Worksheet {...worksheetConfig} dataSource={data}>
  156.         </Worksheet>
  157.       </SpreadSheets>
  158.     );
  159.   };


  160.   /**
  161.    * 创建Excel 及 表单
  162.    */
  163.   createSheet = () => {
  164.     const { sheetList } = this.state;
  165.     const {
  166.       form: {
  167.         getFieldDecorator,
  168.       },
  169.     } = this.props;


  170.     return sheetList.map((item, i) => {
  171.       return (
  172.         <Card
  173.           key={i}
  174.           type="inner"
  175.           title=""
  176.           style={{ marginBottom: '10px' }}
  177.           extra={<Button type="danger" onClick={() => this.removeSheet(i)}>删除</Button>}
  178.         >
  179.           {this.createSpreadSheets()}
  180.           <FormItem {...formItemLayout} label="数据同步">
  181.             <Button type='primary'>同步</Button>
  182.           </FormItem>
  183.         </Card>
  184.       );
  185.     });
  186.   };

  187.   /**
  188.    * 添加按钮点击
  189.    */
  190.   addSheet = () => {
  191.     const { sheetList } = this.state;
  192.     this.setState({
  193.       sheetList: ['', ...sheetList],
  194.     });
  195.   };

  196.   /**
  197.    * 表单提交
  198.    * @param e
  199.    */
  200.   handleSubmit = e => {
  201.     const { dispatch, form } = this.props;
  202.     e.preventDefault();
  203.     form.validateFieldsAndScroll((err, values) => {
  204.       if (!err) {
  205.         dispatch({
  206.           type: 'form/submitRegularForm',
  207.           payload: values,
  208.         });
  209.       }
  210.     });
  211.   };

  212.   /**
  213.    * 删除Excel
  214.    * @param e
  215.    */
  216.   removeSheet = e => {
  217.     const { sheetList } = this.state;
  218.     const newSheetList = sheetList.map(item => ({ ...item }));
  219.     newSheetList.splice(e, 1);
  220.     this.setState({
  221.       sheetList: newSheetList,
  222.     });
  223.   };

  224.   /**
  225.    * 文件处理
  226.    */
  227.   normFile = e => {
  228.     if (ss === 1) {
  229.       ++ss;
  230.       console.log('Upload event:', e.file.originFileObj);
  231.       new Excel.IO().open(e.file.originFileObj, (s) => {
  232.         console.log( SpreadSheets, '------');
  233.       }, (s) => {
  234.         // console.log(s, '++');
  235.       }, { password: '' });

  236.       if (Array.isArray(e)) {
  237.         return e;
  238.       }
  239.     }
  240.     return e && e.fileList;
  241.   };


  242.   render() {
  243.     return (
  244.       <PageHeaderWrapper title="添加">
  245.         <Card bordered={false}>
  246.           {this.getFields()}
  247.         </Card>
  248.       </PageHeaderWrapper>
  249.     );
  250.   }
  251. }

  252. export default AddPracticalExercises;
复制代码

回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-1-14 14:18:39
6#
好的,正在处理您的问题,今天之内给您答复。
回复 使用道具 举报
lzynfd
注册会员   /  发表于:2019-1-14 15:43:32
7#
KevinChen 发表于 2019-1-14 14:18
好的,正在处理您的问题,今天之内给您答复。

好的
回复 使用道具 举报
lzynfd
注册会员   /  发表于:2019-1-15 09:01:18
8#
KevinChen 发表于 2019-1-14 14:18
好的,正在处理您的问题,今天之内给您答复。

怎么样了?
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-1-15 11:08:42
9#
您好,请参考API 中关于react的使用方法:

http://help.grapecity.com/spread ... heetswithReact.html

另外,引入的@grapecity/spread-sheets-react 在官网下载的产品包中有源码,位置是:

Spread.Sheets.All.12.0.4\scripts\interop\react

react样例工程位置:

Spread.Sheets.All.12.0.4\samples\ReactSample

评分

参与人数 1满意度 +5 收起 理由
lzynfd + 5

查看全部评分

回复 使用道具 举报
lzynfd
注册会员   /  发表于:2019-1-15 13:08:30
10#
KevinChen 发表于 2019-1-15 11:08
您好,请参考API 中关于react的使用方法:

http://help.grapecity.com/spread/SpreadSheets12/webframe.h ...

我知道用法,
  1. const excelIo = new Excel.IO();
  2.       excelIo.open(e.file.originFileObj, (s) => {
  3.         const sheet = new Spread.Spread.Sheets.Workbook();
  4.         sheet.fromJSON(s);
  5.       }, (s) => {
  6.         // console.log(s, '++');
  7.       }, { password: '' });
复制代码
  1. const spreadSheetsConfig = {
  2.       backColor: '#fff',
  3.       showVerticalScrollbar: true,
  4.       showHorizontalScrollbar: true,
  5.       tabStripVisible: true,
  6.       grayAreaBackColor: '#C0C0C0',

  7.     };
  8.     const worksheetConfig = {
  9.       rowCount: 10,
  10.       colCount: 10,
  11.       name: 'sheet1',
  12.     };

  13.     return (
  14.       <SpreadSheets {...spreadSheetsConfig} hostStyle={{ width: '100%', height: '400px' }}>
  15.         <Worksheet {...worksheetConfig}>
  16.         </Worksheet>
  17.       </SpreadSheets>
  18.     );
复制代码
这里读到文件后,怎样放在表格中???
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部