您好,由于公司电脑文件加密限制,只能贴这个代码了,还请见谅哈。
使用react官方cli命令创建的demo项目:yarn create react-app sjs-react-app
index.js文件
- import React, { useState, useEffect } from 'react';
- import * as GC from '@grapecity-software/spread-sheets';
- import "@grapecity/spread-sheets-io";
- import { SpreadSheets, Worksheet } from '@grapecity-software/spread-sheets-react';
- import '../node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css';
- function App() {
- const [spread, setSpread] = useState()
- const [selectedFile, setSelectedFile] = useState(null)
- let hostStyle = {
- width: '100%',
- height: '600px',
- border: '1px solid darkgray'
- }
- useEffect(() => {
- if (!selectedFile) {
- console.log(111)
- return
- } else {
- console.log(selectedFile, '<<<<selectedFile');
- spread.open(selectedFile, function() {
- console.log('success');
- }, function (info) {
- console.log(info,'error');
- })
- }
- }, [selectedFile])
- let initSpread = function (spread) {
- setSpread(spread)
- // let sheet = spread.getActiveSheet();
- // sheet.getCell(0, 0).vAlign(GC.Spread.Sheets.VerticalAlign.center).value('Hello SpreadJS!');
- }
- const uploadChange = (e) => {
- console.log(e, '<<<<file');
- let file = e.target.files[0];
- setSelectedFile(file)
- }
- return (
- <div className="App">
- <input type="file" onChange={uploadChange} accept=".xls,.xlsx" />
- <SpreadSheets workbookInitialized={spread => initSpread(spread)} hostStyle={hostStyle}>
- <Worksheet></Worksheet>
- </SpreadSheets>
- </div>
- );
- }
- export default App;
复制代码 package.json文件
- {
- "name": "sjs-react-app",
- "version": "0.1.0",
- "private": true,
- "dependencies": {
- "@grapecity-software/spread-sheets": "^17.0.10",
- "@grapecity-software/spread-sheets-io": "^17.0.10",
- "@grapecity-software/spread-sheets-react": "^17.0.10",
- "cra-template": "1.2.0",
- "react": "^18.3.1",
- "react-dom": "^18.3.1",
- "react-scripts": "5.0.1"
- },
- "scripts": {
- "start": "react-scripts start",
- "build": "react-scripts build",
- "test": "react-scripts test",
- "eject": "react-scripts eject"
- },
- "eslintConfig": {
- "extends": [
- "react-app",
- "react-app/jest"
- ]
- },
- "browserslist": {
- "production": [
- ">0.2%",
- "not dead",
- "not op_mini all"
- ],
- "development": [
- "last 1 chrome version",
- "last 1 firefox version",
- "last 1 safari version"
- ]
- }
- }
复制代码
|