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 小时前
POSIX信号量
linux·运维·服务器·后端
油炸自行车3 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
人还是要有梦想的4 小时前
linux下用搜狗输入法,中英文切换
linux·运维·服务器
bush44 小时前
嵌入式linux学习记录二
linux·运维·学习
9分钟带帽4 小时前
linux_通过NFS挂载远程服务器的硬盘
linux·服务器
运维栈记7 小时前
API Error: 400 Request body format invalid
linux·ai
小白兔奶糖ovo7 小时前
【Leetcode】231. 2的幂
linux·算法·leetcode
s_w.h7 小时前
【 linux 】动静态库的制作
linux·运维·服务器·算法·bash
顺风尿一寸8 小时前
深入Linux内核:mkdir系统调用的完整实现解析
linux
用户2367829801688 小时前
Linux free 命令深度解析:从内存监控到 OOM 排查的完整指南
linux