no message

Former-commit-id: c95990a2a34f940a3128bcb4b1bb7dd3fb685457
Former-commit-id: 427f6cad25279c84e889eecd6ab1e5cff21de575
Former-commit-id: 5dd1e8093112faece6b742767a3f0692edd855ff
This commit is contained in:
李杨
2018-02-15 15:38:07 +08:00
parent 28a62b15c0
commit 691a47cc78
2 changed files with 52 additions and 49 deletions

View File

@@ -1,5 +1,7 @@
/* eslint-disable */
// 库
import papa from 'papaparse'
import XLSX from 'xlsx'
export default {
install (Vue, options) {
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)
})
}
}
}