<p-upload
:show-upload-list="false"
:before-upload="(file) => {return importExcel(file)}"
accept=".xls,.xlsx"
>
<p-button
type="primary"
v-if="!edit"
>
批量上传
</p-button>
</p-upload>
const excelmap = [
{key: 'a', title: 'a'},
{key: 'b', title: 'aa'},
{key: 'c', title: 'aa'},
{key: 'd', title: 'aa'},
{key: 'e', title: 'aa'},
{key: 'f', title: 'aa'},
{key: 'g', title: 'aa'},
{key: 'h', title: 'aa'},
{key: 'i', title: 'aa'},
{key: 'j', title: 'aa'},
{key: 'k', title: 'aa'},
{key: 'l', title: 'aa'},
{key: 'm', title: 'aa'},
{key: 'n', title: 'aa'}
]
import * as XLSX from 'xlsx'
importExcel (file) {
console.log('file', file)
const site = this.form.site
if (file.name.indexOf('.xls') === -1 || file.name.indexOf('.xlsx') === -1) {
this.$message.error('请上传xls或xlsx格式的文件')
return false
}
this.loading = true
const reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = (e) => {
const data = e.target.result
const workbook = XLSX.read(data, { type: 'binary' })
const sheetNames = workbook.SheetNames
console.log('sheetNames', sheetNames)
const worksheet = workbook.Sheets[sheetNames[0]]
const timeSheet = workbook.Sheets['特殊耗时']
const json = XLSX.utils.sheet_to_json(worksheet) || []
const length = json.length
console.log('json', json)
console.log('length', length)
if (this.form.site === 't42' && length % 5 !== 0) {
this.$message.error('上传数据行数异常,或存在空行数据,请检查')
this.loading = false
return false
}
if (this.form.site !== 't431' && length % 6 !== 0) {
this.$message.error('上传数据行数异常,或存在空行数据,请检查')
this.loading = false
return false
}
this.exceltableList = this.parsingTable(worksheet)
if (this.form.site !== 't45'&&timeSheet) {
this.timetableList = this.parsingTable(timeSheet)
const regex = new RegExp(/^[1-9]\d{3}-\d{2}$/)
this.exceltableList = this.exceltableList.map((item, index) => {
let obj = { ...item }
let timeObj = this.timetableList[index]||{}
for (let key in timeObj) {
if (regex.test(key)) {
obj[`${key}-time`] = timeObj[key]
}
}
return obj
})
}
console.log('exceltableList', this.exceltableList)
}
reader.onerror = (e) => {
this.$message.error('文件解析失败')
this.loading = false
}
return false
},
parsingTable (table) {
console.log('table', table)
let header = []
let dataSource = []
let maxRowIndex = 0
let mapHeader = []
let keys = Object.keys(table)
const range = XLSX.utils.decode_range(table['!ref'])
maxRowIndex = range['e']['r'] - range['s']['r']
for (let [i, h] of keys.entries()) {
let col = h.replace(/[^A-Z]/g, '')
h.indexOf('!') === -1 && header.indexOf(col) === -1 && header.push(col)
if (
(!table['!ref'] || !table['!ref'].includes(':')) &&
header.some((c) => table[`${c}${i}`])
) {
maxRowIndex = i > maxRowIndex ? i : maxRowIndex
}
}
for (let index = 1; index <= maxRowIndex + 1; index++) {
let row = []
row = header.map((item, i) => {
let key = `${item}${index}`
let cell = table[key] || {}
if (index === 1) {
if (this.form.site === 't45') {
mapHeader.push(
excelmap.find((a) => cell?.w?.includes(a.title))?.key || cell.w?.trim()
)
} else {
mapHeader.push(
t35excelmap.find((a) => cell?.w?.includes(a.title))?.key || cell.w?.trim()
)
}
}
return {
key,
name: cell ? (cell.w?.trim() || '') : '',
keyMap: mapHeader[i]
}
})
dataSource.push(row)
}
if (table['!merges']) {
for (let item of table['!merges']) {
for (let r = item.s.r; r <= item.e.r; r++) {
for (let c = item.s.c; c <= item.e.c; c++) {
let rowIndex = r + 1
let cell = dataSource[r].find((a) => a.key === `${header[c]}${rowIndex}`)
if (cell) {
if (c === item.s.c && r === item.s.r) {
cell.rowspan = item.e.r - item.s.r + 1
cell.colspan = item.e.c - item.s.c + 1
} else {
cell.rowspan = 0
cell.colspan = 0
}
}
}
}
}
}
console.log(dataSource, 'dataSource')
console.log(header, 'header')
console.log(mapHeader, 'mapHeader')
return this.excelDatatrans(dataSource)
},
excelDatatrans (tableList) {
const arr = []
tableList.map((item, index) => {
if (index > 0) {
const obj = {}
item.map((i, ind) => {
obj[i.keyMap] = i.name
if (i.rowspan >= 0) {
obj[`${i.keyMap}_rowspan`] = i.rowspan
}
if (i.colspan >= 0) {
obj[`${i.keyMap}_colspan`] = i.colspan
}
})
arr.push(obj)
}
})
let flag = arr.every((item, index) => {
const keys = Object.keys(item)
const length = keys.length
const filterKeys = keys.filter((i) => i.indexOf('_colspan') === -1 && i.indexOf('_rowspan') === -1)
const filterLength = filterKeys.length
const filterArr = filterKeys.filter((i) => item[i] === '')
const filterArrLength = filterArr.length
if (filterLength === filterArrLength) {
this.$message.error(`第${index + 2}行数据存在空行数据,请检查`)
this.loading = false
return false
}
return true
})
console.log(flag, 'flag')
if (!flag) {
return false
}
this.excelvisible = true
this.loading = false
return this.handleTableData(arr)
},
handleTableData (tableList) {
const arr = []
tableList.map((item, index) => {
const obj = {}
Object.keys(item).map((i, ind) => {
if (i.indexOf('_rowspan') > -1 && item[i] === 0) {
const key = i.split('_rowspan')[0]
obj[key] = arr[index - 1][key]
} else {
obj[i] = item[i]
}
})
arr.push(obj)
})
console.log(arr, 'arr')
return arr
},