1.写一个上传
这里直接用upload组件即可
javascript
<Upload {...uploadProps} maxCount={1} accept={".xlsx"}>
<Button icon={<UploadOutlined />}>
{`${formatMessage({id: 'clk_upload'}, {file: formatMessage({id: 'excel_file'})})}`}
</Button>
</Upload>
2.写props
javascript
const uploadProps: any = {
onRemove: (file: any) => {
const index = fileList.indexOf(file);
const newFileList = fileList.slice();
newFileList.splice(index, 1);
setFileList(newFileList);
},
beforeUpload: (file: any) => {
let reader = new FileReader();
reader.onload = function(event: any) {
let data = new Uint8Array(event.target.result);
let workbook = XLSX.read(data, {type: 'array'});
let worksheet = workbook.Sheets[workbook.SheetNames[0]];
let jsonData = XLSX.utils.sheet_to_json(worksheet, {header: 1});
reader.readAsArrayBuffer(file);
setFileList(file?.name?.includes('xlsx') ? [file]: []);
return false;
},
fileList
};
beforeUpload函数里处理上传的数据转成Uint8Array,读取文件中的第一个表,使用.utils.sheet_to_json方法读出json数据,具体在进行处理即可。