1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <template> <div> <q-uploader url="/ht/file/upload" color="teal" flat bordered style="max-width:300px;" max-files="1" accept=".xls,.xlsx" @added="added" @uploading="uploading" fieldName="file" no-thumbnails/> <q-space /> </div> </template> <script> import XLSX from "xlsx"; export default { data(){ return{ } }, methods:{ uploading(val){ console.log(val.xhr.response); }, added(val) { let fileName = ""; if (val.length <= 0) { return false; } else if (!/\.(xls|xlsx)$/.test(val[0].name.toLowerCase())) { this.$q.notify({position: 'center',type:"negative",message:'上传格式不正确,请上传xls或者xlsx格式',timeout:3000}); return false; } else { fileName = val[0].name; }
const fileReader = new FileReader(); fileReader.onload = ev => { try { const data = ev.target.result; const workbook = XLSX.read(data, { type: "binary" });
let bookName = workbook.SheetNames[0]; let ws = workbook.Sheets[bookName]; const d = XLSX.utils.sheet_to_json(ws, {header: 1});
console.log(d); } catch (e) { this.$q.notify({position: 'center',type:"negative",message:'表格解析失败!',timeout:3000}); return false; } }; fileReader.readAsBinaryString(val[0]); } } } </script>
|