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

相关推荐
叶半欲缺6 分钟前
Linux通过lvm扩容根目录
linux·运维·服务器
utf8mb4安全女神1 小时前
Linux网络服务
linux·运维·服务器
vortex51 小时前
Linux PAM 配置详解:从原理到实战,彻底阻断非授权提权
java·linux·服务器
ZPC82101 小时前
Linux Preempt-RT 实时内核 ** 抖动(Jitter)** 完整测试方法
linux·运维·服务器
2501_920047031 小时前
openclaw在ubuntu系统的安装
linux·运维·ubuntu·openclaw
一个人旅程~2 小时前
linux如何“抢”过windows的usb移动硬盘权限对0磁道损坏的移动硬盘进行尝试修复
linux·windows·经验分享·电脑
sukioe3 小时前
Linux RPM 方式安装 MySQL 8.0
linux·mysql·adb
Bert.Cai3 小时前
Linux tee命令详解
linux·运维·服务器
宋浮檀s4 小时前
应急响应(系统日志)
linux·运维·网络安全·应急响应
cui_ruicheng4 小时前
Linux网络编程(七):TCP Socket编程与EchoServer
linux·服务器·网络·tcp/ip