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

相关推荐
霞姐聊IT6 分钟前
三大并发技术—进程、线程和协程
linux·运维·网络·操作系统
南境十里·墨染春水22 分钟前
linux学习进展 网络编程——HTTPS (补充)
linux·网络·学习
t5y2227 分钟前
【Linux】学习小计
linux
老四啊laosi40 分钟前
【Linux系统】18. 基础IO(文件管理部分)
linux·文件操作·重定向
mounter6251 小时前
深度解析 dmabuf/devmem:从图形渲染到 AI 与高性能网络的演进之路
linux·网络·人工智能·内存管理·kernel
北山有鸟1 小时前
RK3588利用NPU加速Lens Shading Correction参数生成
linux
酿情师1 小时前
FinalShell 下载与安装指南
linux·服务器·windows·ssh
灵晔君1 小时前
【Linux】进程(二)
linux·运维·服务器
JasonFreeLab1 小时前
DeepSeek TUI 安装配置极速指南(Win / Mac / Linux)
linux·运维·macos·ai·ai编程·ai写作
原来是猿1 小时前
TCP Echo Server 深度解析:从单进程到线程池的演进之路(下)
linux·服务器·数据库