获取xlsx文件列名
npm install xlsx --save-dev
import * as XLSX from 'xlsx/xlsx.mjs'
const readFile = (file) => {
return new Promise((resolve) => {
const reader = new FileReader()
reader.readAsBinaryString(file)
reader.onload = (ev) => {
resolve(ev.target.result)
}
})
}
async function handle(ev) {
const file = ev.originFileObj
if (!file) {
message.error('文件打开失败')
} else {
const data = await readFile(file)
const workbook = XLSX.read(data, { type: 'binary' })
const wsname = workbook.SheetNames[0]
const headerData = XLSX.utils.sheet_to_json(workbook.Sheets[wsname], { header: 1 })[0]
setOption(headerData)
}
}
const props = {
name: 'file',
maxCount: 1,
action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
headers: {
Origin: '',
},
beforeUpload: sizeCheck,
onChange(info) {
if (info.file.status !== 'uploading') {
console.log(info.file, info.fileList)
}
if (info.file.status === 'done') {
handle(info.file)
message.success(`${info.file.name} file uploaded successfully`)
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`)
}
},
}
<Upload {...props}>
<Button icon={<UploadOutlined />}>点击上传</Button>
</Upload>