no message
Former-commit-id: c95990a2a34f940a3128bcb4b1bb7dd3fb685457 Former-commit-id: 427f6cad25279c84e889eecd6ab1e5cff21de575 Former-commit-id: 5dd1e8093112faece6b742767a3f0692edd855ff
This commit is contained in:
@@ -32,8 +32,6 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
/* eslint-disable */
|
|
||||||
import XLSX from 'xlsx'
|
|
||||||
export default {
|
export default {
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
@@ -47,7 +45,9 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
generateDate ({header, results}) {
|
handleUpload (file) {
|
||||||
|
this.$import.excel(file)
|
||||||
|
.then(({header, results}) => {
|
||||||
this.table.columns = header.map(e => {
|
this.table.columns = header.map(e => {
|
||||||
return {
|
return {
|
||||||
label: e,
|
label: e,
|
||||||
@@ -55,46 +55,9 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.table.data = results
|
this.table.data = results
|
||||||
},
|
})
|
||||||
handleUpload(file) {
|
|
||||||
this.readerData(file)
|
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
readerData(file) {
|
|
||||||
const reader = new FileReader()
|
|
||||||
const fixdata = data => {
|
|
||||||
let o = ''
|
|
||||||
let l = 0
|
|
||||||
const w = 10240
|
|
||||||
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
|
|
||||||
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
const get_header_row = sheet => {
|
|
||||||
const headers = []
|
|
||||||
const range = XLSX.utils.decode_range(sheet['!ref'])
|
|
||||||
let C
|
|
||||||
const R = range.s.r
|
|
||||||
for (C = range.s.c; C <= range.e.c; ++C) {
|
|
||||||
var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
|
|
||||||
var hdr = 'UNKNOWN ' + C
|
|
||||||
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
|
|
||||||
headers.push(hdr)
|
|
||||||
}
|
|
||||||
return headers
|
|
||||||
}
|
|
||||||
reader.onload = e => {
|
|
||||||
const data = e.target.result
|
|
||||||
const fixedData = fixdata(data)
|
|
||||||
const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
|
|
||||||
const firstSheetName = workbook.SheetNames[0]
|
|
||||||
const worksheet = workbook.Sheets[firstSheetName]
|
|
||||||
const header = get_header_row(worksheet)
|
|
||||||
const results = XLSX.utils.sheet_to_json(worksheet)
|
|
||||||
this.generateDate({ header, results })
|
|
||||||
}
|
|
||||||
reader.readAsArrayBuffer(file)
|
|
||||||
},
|
|
||||||
download () {
|
download () {
|
||||||
window.location.href = 'http://fairyever.qiniudn.com/d2-admin-import-xlsx-demo.xlsx'
|
window.location.href = 'http://fairyever.qiniudn.com/d2-admin-import-xlsx-demo.xlsx'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
/* eslint-disable */
|
||||||
// 库
|
// 库
|
||||||
import papa from 'papaparse'
|
import papa from 'papaparse'
|
||||||
|
import XLSX from 'xlsx'
|
||||||
export default {
|
export default {
|
||||||
install (Vue, options) {
|
install (Vue, options) {
|
||||||
Vue.prototype.$import = {
|
Vue.prototype.$import = {
|
||||||
@@ -14,6 +16,44 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
// 导入 excel
|
||||||
|
excel (file) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader()
|
||||||
|
const fixdata = data => {
|
||||||
|
let o = ''
|
||||||
|
let l = 0
|
||||||
|
const w = 10240
|
||||||
|
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
|
||||||
|
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
const getHeaderRow = sheet => {
|
||||||
|
const headers = []
|
||||||
|
const range = XLSX.utils.decode_range(sheet['!ref'])
|
||||||
|
let C
|
||||||
|
const R = range.s.r
|
||||||
|
for (C = range.s.c; C <= range.e.c; ++C) {
|
||||||
|
var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
|
||||||
|
var hdr = 'UNKNOWN ' + C
|
||||||
|
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
|
||||||
|
headers.push(hdr)
|
||||||
|
}
|
||||||
|
return headers
|
||||||
|
}
|
||||||
|
reader.onload = e => {
|
||||||
|
const data = e.target.result
|
||||||
|
const fixedData = fixdata(data)
|
||||||
|
const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
|
||||||
|
const firstSheetName = workbook.SheetNames[0]
|
||||||
|
const worksheet = workbook.Sheets[firstSheetName]
|
||||||
|
const header = getHeaderRow(worksheet)
|
||||||
|
const results = XLSX.utils.sheet_to_json(worksheet)
|
||||||
|
resolve({header, results})
|
||||||
|
}
|
||||||
|
reader.readAsArrayBuffer(file)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user