| <template> |
| <span> |
| <input class="input-file" type="file" @change="exportData" accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/> |
| <button @click="btnClick">导入EXCEL</button> |
| </span> |
| </template> |
| <script> |
| import XLSX from 'xlsx' |
| export default { |
| name: 'HelloWorld', |
| props: { |
| type: String, |
| default: '选择excel文件' |
| }, |
| methods: { |
| btnClick () { |
| document.querySelector('.input-file').click() |
| }, |
| exportData (event) { |
| if (!event.currentTarget.files.length) { |
| return |
| } |
| const that = this |
| |
| var f = event.currentTarget.files[0] |
| |
| var reader = new FileReader() |
| |
| FileReader.prototype.readAsBinaryString = function (f) { |
| var binary = '' |
| var wb |
| var outdata |
| var reader = new FileReader() |
| reader.onload = function (e) { |
| |
| var bytes = new Uint8Array(reader.result) |
| var length = bytes.byteLength |
| for (var i = 0; i < length; i++) { |
| binary += String.fromCharCode(bytes[i]) |
| } |
| |
| wb = XLSX.read(binary, { |
| type: 'binary' |
| }) |
| outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) |
| |
| console.log('outdata = ' + JSON.stringify(outdata)) |
| that.$emit('getResult', outdata) |
| } |
| reader.readAsArrayBuffer(f) |
| } |
| reader.readAsBinaryString(f) |
| } |
| } |
| } |
| </script> |
| |
| <style scoped> |
| .input-file { |
| display: none; |
| } |
| </style> |