经常做文件上传,所以有时候要预览上传文件的内容,之前是后端读取的,现在使用js-xlsx,前端也可以读取
xlsx是一款非常方便的只需要纯JS即可读取和导出excel的工具库,功能强大,支持格式众多,支持xls、xlsx、csv等十几种格式。本文随意以这3种文件格式为例
首先要安装 xlsx库
引入import XLSX from 'xlsx'
批量上传
1、通过input[type="file"]标签获取本地文件File对象
handleUpload() {
this.$refs['excel-upload-input'].click();
},
handleClick(e) {
const files = e.target.files;
const rawFile = files[0]; // only use files[0]
if (!rawFile) return;
this.upload(rawFile);
},
upload(rawFile) {
this.$refs['excel-upload-input'].value = null; //
this.readerData(rawFile);
},
readerData(rawFile) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
//使用Fi