vue之解析Excel表格
安装xlsx
npm i xlsx
使用
<div>
<input
type="file"
accept=".xlsx,.xls"
@change="readWorkbook($event)"
/>
</div>import * as XLSX from 'xlsx';
data() {
return {
files: [],
filename: ''
}
}
async readWorkbook(e) {
this.files = e.target.files
this.filename = this.files[0].name
try {
const file = this.files[0]
const buffer = await file.arrayBuffer()
console.log('buffer: ', buffer);
const workbook = XLSX.read(buffer, {type:'array'});
console.log('workbook: ', workbook);
const wsname = workbook.SheetNames[0]
// const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname], {header: 1})
// {header: 1}会转换成数组,但是会保留空白行 defval: '' 设置默认值占位
// 可使用['word','word']自定义字段名称
const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname], {header: ['name','phone','sex','department']})
console.log('ws: ', ws);
} catch (error) {
throw error
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 晨曦BlogTour🎶!
评论