安装依赖
首先,你需要安装xlsx
库和antd
库:
npm install xlsx antd
创建React组件
接下来,创建一个React组件来处理文件上传和读取Excel文件。
import React, { useState } from 'react'; import { Upload, Button, message } from 'antd'; import XLSX from 'xlsx'; const ExcelUploader = () => { const [data, setData] = useState([]); const handleUpload = ({ file }) => { const reader = new FileReader(); reader.onload = (event) => { try { const binaryString = event.target.result; const workbook = XLSX.read(binaryString, { type: 'binary' }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const jsonData = XLSX.utils.sheet_to_json(sheet); setData(jsonData); message.success('文件上传成功'); } catch (error) { message.error('文件读取失败,请检查文件格式是否正确'); console.error('读取文件失败', error); } }; reader.readAsBinaryString(file); }; const beforeUpload = (file) => { const validFormats = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']; if (!validFormats.includes(file.type)) { message.error('请选择一个有效的Excel文件'); return false; } return true; }; return ( <div> <Upload beforeUpload={beforeUpload} customRequest={handleUpload} showUploadList={false} > <Button icon="upload">上传Excel文件</Button> </Upload> <pre>{JSON.stringify(data, null, 2)}</pre> </div> ); }; export default ExcelUploader;
代码解释
-
安装与引入:
- 使用
npm install xlsx antd
安装xlsx
和antd
库。 - 在React组件中引入
Upload
、Button
、message
组件和XLSX
库:
- 使用
import { Upload, Button, message } from 'antd'; import XLSX from 'xlsx';
-
文件上传处理:
- 使用
Upload
组件创建一个文件上传按钮。 beforeUpload
函数用于校验文件格式,确保用户上传的是有效的Excel文件。customRequest
函数用于处理文件上传逻辑,读取文件内容并转换为JSON格式。
- 使用
-
读取Excel文件:
- 使用
FileReader
对象读取用户选择的文件。readAsBinaryString
方法将文件内容读取为二进制字符串。 - 使用
XLSX.read
方法读取二进制字符串,并将其转换为工作簿对象。 - 获取第一个工作表的名称和内容。
- 使用
XLSX.utils.sheet_to_json
方法将工作表的内容转换为JSON格式。
- 使用
-
展示数据:
- 使用
<pre>{JSON.stringify(data, null, 2)}</pre>
将读取到的数据以格式化的JSON形式展示在页面上。
- 使用
样式设置
为了使上传按钮更符合Ant Design的样式规范,可以使用Ant Design的预设样式类。以下是一个示例,展示如何使用Ant Design的样式类来美化上传按钮:
import React, { useState } from 'react'; import { Upload, Button, message } from 'antd'; import XLSX from 'xlsx'; import './ExcelUploader.css'; // 引入自定义样式文件 const ExcelUploader = () => { const [data, setData] = useState([]); const handleUpload = ({ file }) => { const reader = new FileReader(); reader.onload = (event) => { try { const binaryString = event.target.result; const workbook = XLSX.read(binaryString, { type: 'binary' }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const jsonData = XLSX.utils.sheet_to_json(sheet); setData(jsonData); message.success('文件上传成功'); } catch (error) { message.error('文件读取失败,请检查文件格式是否正确'); console.error('读取文件失败', error); } }; reader.readAsBinaryString(file); }; const beforeUpload = (file) => { const validFormats = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']; if (!validFormats.includes(file.type)) { message.error('请选择一个有效的Excel文件'); return false; } return true; }; return ( <div className="upload-container"> <Upload beforeUpload={beforeUpload} customRequest={handleUpload} showUploadList={false} > <Button type="primary" icon="upload">上传Excel文件</Button> </Upload> <pre className="data-preview">{JSON.stringify(data, null, 2)}</pre> </div> ); }; export default ExcelUploader;
自定义样式文件 ExcelUploader.css
.upload-container { margin: 20px; text-align: center; } .data-preview { margin-top: 20px; background-color: #f0f0f0; padding: 10px; border-radius: 4px; overflow: auto; max-height: 400px; }
代码解释
-
自定义样式文件:
- 创建一个
ExcelUploader.css
文件,定义上传按钮和数据预览区域的样式。 - 使用
className
属性将自定义样式应用到React组件中。
- 创建一个
-
Ant Design样式类:
- 使用
Button
组件的type="primary"
属性,使按钮具有Ant Design的主色调。 - 使用
icon="upload"
属性,添加上传图标。
- 使用
总结
通过使用antd
的Upload
组件和xlsx
库,你可以在React项目中高效地实现Excel文件的上传和读取功能。同时,通过自定义样式文件,可以确保组件的样式符合Ant Design的设计规范。
到此这篇关于在React中使用Antd上传并读取Excel文件的详细步骤的文章就介绍到这了,更多相关React使用Antd上传并读取Excel内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源链接:https://www.jb51.net/javascript/334234q17.htm
© 版权声明
本站所有资源来自于网络,仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您(转载者)自己承担!
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
THE END
暂无评论内容