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

相关推荐
Y淑滢潇潇8 分钟前
RHCE 防火墙实验
linux·运维·rhce
wadesir35 分钟前
当前位置:首页 > 服务器技术 > 正文Linux网络HSRP协议(实现路由器热备份与高可用性的实用指南)
linux·服务器·网络
稻谷君W38 分钟前
Ubuntu 远程访问 Win11 WSL2 并固定访问教程
linux·运维·ubuntu
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [kernel]workqueue
linux·笔记·学习
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [kernel]usermode_helper
linux·笔记·学习
weixin_436525071 小时前
使用 idea 命令行构建 Docker 镜像并部署到云服务器
linux·docker·github
lqz19932 小时前
根据html导出excel和word
html·word·excel
12程序猿3 小时前
postman调用文件(.xlsm---带宏的excel文件)下载接口成功下载excel文件,浏览器访问下载文件打不开
excel·lua·postman
z***94843 小时前
Linux下安装Nginx服务及systemctl方式管理nginx详情
linux·运维·nginx
凉晓风3 小时前
Linux上TCP通信异常排查工具命令
linux·运维·tcp/ip