antd本地上传excel文件并读取文件的数据转为json

编程入门 行业动态 更新时间:2024-10-28 10:26:10

antd本地上传excel<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件并读取文件的数据转为json"/>

antd本地上传excel文件并读取文件的数据转为json

1.写一个上传

这里直接用upload组件即可

<Upload {...uploadProps} maxCount={1} accept={".xlsx"}><Button icon={<UploadOutlined />}>{`${formatMessage({id: 'clk_upload'}, {file: formatMessage({id: 'excel_file'})})}`}</Button></Upload>

2.写props

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

更多推荐

antd本地上传excel文件并读取文件的数据转为json

本文发布于:2023-11-16 09:51:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616807.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   上传   数据   antd   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!