当前位置:网站首页>antd组件upload上传xlsx文件,并读取文件内容
antd组件upload上传xlsx文件,并读取文件内容
2022-07-02 22:09:00 【不会秃头】
xlsx包
这个包是个npm包,这里使用的版本为:
“xlsx”: “^0.14.5”
使用方式
首先,在文件中引入:
import XLSX from 'xlsx';
然后在upload组件的beforeUpload中使用:
beforeUpload(files) {
const readFile = file => {
const [fileReader, data1] = [new FileReader(), {
}];
let [binaryData, workbook] = [null, null];
fileReader.onload = e => {
binaryData = e.target.result;
workbook = XLSX.read(binaryData, {
type: 'binary' });
Object.keys(workbook.Sheets).forEach(sheet => {
data1[sheet] = XLSX.utils
.sheet_to_json(workbook.Sheets[sheet], {
header: 1 })
.slice(1);
});
workbook.SheetNames.forEach(sheetName => {
const dataSheet = data1[sheetName];
for (let index = 0; index < dataSheet.length; index += 1) {
const row = dataSheet[index];
const item = {
name: row[0] ? row[0] : null,
snCode: row[1] ? row[1] : null,
macAddress: row[2] ? row[2] : null,
ipAddress: row[3] ? row[3] : null,
number: Number(row[4] ? row[4] : null),
podium: (row[5] ? row[5] : null) === '是' ? true : false,
devices: [
{
ipAddress: row[6] ? row[6] : null,
location: 'top1'
},
{
ipAddress: row[7] ? row[7] : null,
location: 'side1'
},
{
ipAddress: row[8] ? row[8] : null,
location: 'front1'
},
{
ipAddress: row[9] ? row[9] : null,
location: 'front2'
},
]
};
fileData.push(item);
}
});
// fileData即为表格数据
if (fileData.length > 0) {
that.setState({
tables: fileData,
});
message.success('导入成功!');
}else {
message.error('导入失败!')
}
};
fileReader.readAsBinaryString(file);
};
readFile(uploadFile);
边栏推荐
- 送给即将工作的自己
- Socket socket c/s end process
- The kth largest element in the [leetcode] array [215]
- WebRTC音视频采集和播放示例及MediaStream媒体流解析
- php优化foreach中的sql查询
- 杰理之内置关机电流 1.2uA,之后不能长按开机【篇】
- [chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
- Higher order operation of bits
- 杰理之充电拔出,无法触摸开机【篇】
- 数组进阶提高
猜你喜欢
數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
Mathematical modeling -- graph and network models and methods (I)
数据分析学习记录--用EXCEL完成简单的单因素方差分析
P7072 [CSP-J2020] 直播获奖
NC50965 Largest Rectangle in a Histogram
[羊城杯2020]easyphp
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
odoo13搭建医院HRP环境(详细步骤)
随机推荐
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
Film and television excerpts
悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
WebRTC音视频采集和播放示例及MediaStream媒体流解析
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
【喜欢的诗词】好了歌
Jielizhi, production line assembly link [chapter]
UE4 UI adaptive screen
牛客网:龙与地下城游戏
PHP optimizes SQL queries in foreach
Niuke: Dragon and dungeon games
Golang interview finishing three resumes how to write
Mathematical modeling -- graph and network models and methods (I)
Hanging mirror security won four global infosec awards on rsac2022
Uniapp wechat login returns user name and Avatar
Go 4 modes Singleton
Golang面试整理 三 简历如何书写
杰理之快速触摸不响应问题【篇】
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')