我是React JS的新手。我正在尝试使用React.js上传Excel表格文件并将数据显示到表格中。我从链接中得到了部分参考,但它并不完整。请帮忙解决。 从Excel导入数据并在React组件中显示
我是React JS的新手。我正在尝试使用React.js上传Excel表格文件并将数据显示到表格中。我从链接中得到了部分参考,但它并不完整。请帮忙解决。 从Excel导入数据并在React组件中显示
这里有一个完美的库可以实现这个功能!它首先将Excel数据转换为JSON,然后将其渲染为HTML表格。它叫做react-excel-renderer
Install it
npm install react-excel-renderer --save
Import both components ExcelRenderer and OutTable
import {ExcelRenderer, OutTable} from 'react-excel-renderer';
Provide the file object to the ExcelRenderer function in your event handler
fileHandler = (event) => {
let fileObj = event.target.files[0];
//just pass the fileObj as parameter
ExcelRenderer(fileObj, (err, resp) => {
if(err){
console.log(err);
}
else{
this.setState({
cols: resp.cols,
rows: resp.rows
});
}
});
}
Once JSON is obtained, provide it to the OutTable component
<OutTable data={this.state.rows} columns={this.state.cols} tableClassName="ExcelTable2007" tableHeaderRowClass="heading" />
https://github.com/ashishd751/excel-renderer-demo/blob/master/src/App.js
我在openFileBrowser()函数中添加了fileHandler()函数。openFileBrowser = () => {
axios({
url:'http://127.0.0.1:5000/display',
method:'GET',
responseType: 'blob'
})
.then((response) => {
const url = (new File([response.data], "file.xlsx", {type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", lastModified:new Date().getTime()}));
console.log(url);
let fileName = url.name;
console.log(url);
//check for file extension and pass only if it is .xlsx and display error message otherwise
if(fileName.slice(fileName.lastIndexOf('.')+1) === "xlsx"){
this.setState({
uploadedFileName: fileName,
isFormInvalid: false
});
this.renderFile(url)
}
else{
this.setState({
isFormInvalid: true,
uploadedFileName: ""
})
}
})
}
axios.get("/display", {
//url:'http://127.0.0.1:5000/display',
method:'GET',
responseType: 'blob'
})
然后,在 package.json 中添加 "proxy": "http://127.0.0.1:5000"。 - tejeshimport React, { useState } from "react";
import "./App.css";
import * as XLSX from "xlsx";
function App() {
const [items, setItems] = useState([]);
const readExcel = (file) => {
const promise = new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsArrayBuffer(file);
fileReader.onload = (e) => {
const bufferArray = e.target.result;
const wb = XLSX.read(bufferArray, { type: "buffer" });
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
const data = XLSX.utils.sheet_to_json(ws);
resolve(data);
};
fileReader.onerror = (error) => {
reject(error);
};
});
promise.then((d) => {
setItems(d);
});
};
return (
<div>
<input
type="file"
onChange={(e) => {
const file = e.target.files[0];
readExcel(file);
}}
/>
<table class="table container">
<thead>
<tr>
<th scope="col">InputA</th>
<th scope="col">InputB</th>
</tr>
</thead>
<tbody>
{items.map((d) => (
<tr key={d.InputA}>
<th>{d.InputA}</th>
<td>{d.InputB}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default App;
您可以使用像https://react-dropzone.js.org/这样的库来上传文件,然后使用https://github.com/felixrieseberg/React-Spreadsheet-Component库来显示它。
需要datetime模块
const parseDate = v => {
const d = v - 1;
const t = Math.round((d - Math.floor(d)) * 24 * 60 * 60);
console.log('parseDate d ', d, 't', t);
return moment(new Date(1900, 0, d, 0, 0, t)).format('YYYY-MM-DD');
};
const dateStr = parseDate(43523);
console.log('dateStr ', dateStr)
<script src="https://momentjs.com/downloads/moment.js"></script>