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

相关推荐
林鸿群9 分钟前
Ubuntu 26.04 本地安装 GitLab CE 完整教程(非 Docker 方式)
linux·ubuntu·gitlab·私有部署·代码托管·ubuntu 26.04·omnibus
开开心心就好24 分钟前
免费轻量级PDF阅读器,打开速度快
windows·计算机视觉·visualstudio·pdf·计算机外设·excel·myeclipse
勇闯逆流河28 分钟前
【Linux】Linux进程概念(进程优先级,进程切换详解)
linux·运维·服务器
老师好,我是刘同学30 分钟前
30个核心Linux命令速查手册
linux
fsj2009yx33 分钟前
如何把无公网的求生之路2服务器借助VPS转发注册到steam master列表中
linux·wireguard·求生之路2
慵懒的猫mi37 分钟前
deepin UOS AI 助手接入飞书(Feishu)配置指南
linux·人工智能·ai·gpt-3·飞书·文心一言·deepin
Jiozg38 分钟前
ES安装到linux(ubuntu)
linux·ubuntu·elasticsearch
面对疾风叭!哈撒给40 分钟前
Linux之docker-compose使用(redis、nginx、tdengine、java应用)
linux·redis·docker
zzzsde41 分钟前
【Linux】进程(6):程序地址空间
linux·运维·服务器
慵懒的猫mi42 分钟前
deepin UOS AI 助手接入钉钉(DingTalk)配置指南
linux·数据库·人工智能·ai·钉钉·deepin