上传Excel
<Upload
ref="uploadRef"
name="file"
:custom-request="handleChange"
:showUploadList="false"
accept=".xls, .xlsx"
>
<Button type="default"> 点击上传 </Button>
</Upload>
function handleChange(info: any) {
fileList.value = info;
const file: File = fileList.value?.file;
if (!file || !file.name.endsWith('.xlsx')) {
createMessage.error('请选择有效的 .xlsx 文件');
return;
}
const reader = new FileReader();
reader.onload = (e: any) => {
if (e.target && e.target.result) {
const data = e.target.result;
const workbook = XLSX.read(data, { type: 'binary' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const excelData = XLSX.utils.sheet_to_json(worksheet);
console.log('模板数据----->', excelData);
createMessage.success(t('component.upload.uploadSuccess'));
closeModal();
emit('success');
emit('handle-import', excelData);
} else {
createMessage.error('读取结果为空');
}
};
// 读取文件
reader.readAsArrayBuffer(file);
}
标签:const,target,excelData,createMessage,前端,Excel,file,reader,上传
From: https://blog.csdn.net/qq_42748805/article/details/144451223