antd本地上传excel文件并读取文件的数据转为json

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数据,具体在进行处理即可。

相关推荐
小绫网络安全3 小时前
Kali Linux渗透测试入门教程:从零搭建到实战攻防
linux·运维·服务器
西安景驰电子3 小时前
《PTP精确时间协议系列》第一篇:从原理到应用,全面解读IEEE 1588
linux·运维·服务器·开发语言·网络·windows·php
猫吃了源码3 小时前
k9s简介和安装
linux·docker·kubernetes
小马同学-5 小时前
高可用-Keepalived全解析
linux·运维
Forever Nore5 小时前
LeetCode 13 罗马数字转整数 - 按规则处理
linux·服务器·leetcode
我不会插花弄玉5 小时前
2.Linux基础指令下【由浅入深-Linux】
linux·服务器
雯宝7 小时前
3. 驱动框架(块设备)
linux
静止了 所有的花开7 小时前
Ubuntu 26.04 静态 IP 配置不生效解决方法
linux·ubuntu·静态ip
caimouse8 小时前
ReactOS 图形系统分析(3):显示驱动 DDI framebuf.dll
linux·运维·网络
科技林总8 小时前
Hermes-Agentv-0.20.0
linux·运维·服务器