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

相关推荐
---学无止境---4 分钟前
Linux内存管理揭秘:页表递归清理与TLB优化机制
linux
jarreyer26 分钟前
【ubuntu离线安装Oracle 客户端】ldd /opt/oracle/instantclient_19_8/libclntsh.so
linux·ubuntu·oracle
vvw&33 分钟前
如何在 Ubuntu 上安装 PostgreSQL
linux·运维·服务器·数据库·ubuntu·postgresql
小跌—1 小时前
Linux:多路转接
linux·网络
csdn_aspnet1 小时前
如何在 Mac、Ubuntu、CentOS、Windows 上安装 MySQL 客户端
linux·windows·mysql·macos·centos
铭哥的编程日记1 小时前
【Linux网络】传输层协议UDP
linux·网络·udp
x_lrong1 小时前
Linux虚拟机配置jupyter环境并在宿主机访问
linux·运维·笔记·jupyter·虚拟机
羚羊角uou2 小时前
【Linux网络】Socket编程TCP-实现Echo Server(上)
linux·运维·服务器
爱奥尼欧2 小时前
【Linux笔记】网络部分——数据链路层mac-arp
linux·网络·笔记
Evan_ZGYF丶2 小时前
深入解析CFS虚拟运行时间:Linux公平调度的核心引擎
linux·驱动开发·嵌入式·bsp