-
导入xlsx工具包
<script src="/js/xlsx.min.js"></script>
-
上传文件
<el-upload class="upload" ref="excelUpload" style="margin-left:10px" :show-file-list="false" action="#" :auto-upload="false" :multiple="false" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" :on-change="importExcel" :limit="1"> <el-button size="mini" type="primary" plain>数据导入</el-button> </el-upload>
-
解析
// excel导入 importExcel(param) { let _this = this; _this.file2Xce(param).then(item => { if (item && item.length > 0) { // xlsxJson就是解析出来的json数据,数据格式如下 // [{sheetName: sheet1, sheet: sheetData }] if (item[0] && item[0].sheet && item[0].sheet.length) { _this.excelData = item[0].sheet //把数据塞到表格预览 } } _this.$refs.excelUpload.clearFiles(); }).catch((err) => { console.log(err) }); }, /** * 解析文件 * @param {Object} file */ file2Xce(file) { return new Promise(function (resolve, reject) { const reader = new FileReader(); reader.onload = function (e) { const data = e.target.result; this.wb = XLSX.read(data, { type: "binary" }); const result = []; this.wb.SheetNames.forEach(sheetName => { result.push({ sheetName: sheetName, sheet: XLSX.utils.sheet_to_json(this.wb.Sheets[sheetName]) }); }); // console.log("解析") // console.log(result) resolve(result); }; reader.readAsBinaryString(file.raw); }); },
评论