
实用vue进行前台解析数据,直接上代码。
AI-摘要
大橘 GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
实用vue进行前台解析数据,直接上代码。
类似实现可见看 链接
代码使用xlsx
<template>
<div>
<input ref="excel-upload-input" class="excel-upload-input" type="file" accept=".xlsx, .xls" @change="handleClick">
<div class="drop" @drop="handleDrop" @dragover="handleDragover" @dragenter="handleDragover">
拖入文件
<h-button :loading="loading" style="float:right;margin-top: 14px;margin-right: 5px;width:90px;" type="primary" @click="handleUpload">
导入文件
</h-button>
</div>
</div>
</template>
<script>
import { range } from 'lodash'
import XLSX from 'xlsx'
export default {
props: {
beforeUpload: Function, // eslint-disable-line
onSuccess: Function,// eslint-disable-line
// 表格事件
onPageAction: {
type: Function,
required: true,
},
rowidx : {
type: Number,
default: 0
},
nameToKeyMap: {
type: Object,
required: true,
},
},
data() {
return {
loading: false,
excelData: {
header: null,
results: null
}
}
},
methods: {
generateData({ header, results }) {
const nameKeyMap = Object.entries(this.nameToKeyMap);
this.excelData.header = header
let dataJson = JSON.stringify(results);
nameKeyMap.forEach( row => {
dataJson = dataJson.replaceAll('"'+row[0]+'"','"'+row[1]+'"')
// debugger
})
dataJson = dataJson.replaceAll('"/"','""')
this.excelData.results = JSON.parse(dataJson);
this.onSuccess && this.onSuccess(this.excelData)
this.$emit("onPageAction", "getFileData", header, this.excelData.results);
},
handleDrop(e) {
e.stopPropagation()
e.preventDefault()
if (this.loading) return
const files = e.dataTransfer.files
if (files.length !== 1) {
this.$hMessage.warning({
content: '只能上传一个文件',
duration: 5,
closable: true,
})
// this.$message.error('Only support uploading one file!')
return
}
const rawFile = files[0] // only use files[0]
if (!this.isExcel(rawFile)) {
// this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
this.$hMessage.warning({
content: '只能上传xlsx,xls文件',
duration: 5,
closable: true,
})
return false
}
this.upload(rawFile)
e.stopPropagation()
e.preventDefault()
},
handleDragover(e) {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
},
handleUpload() {
this.$refs['excel-upload-input'].click()
},
handleClick(e) {
const files = e.target.files
const rawFile = files[0] // only use files[0]
if (!rawFile) return
if (!this.isExcel(rawFile)) {
// this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
this.$hMessage.warning({
content: '只能上传xlsx,xls文件',
duration: 5,
closable: true,
})
return false
}
this.upload(rawFile)
},
upload(rawFile) {
this.$refs['excel-upload-input'].value = null // fix can't select the same excel
if (!this.beforeUpload) {
this.readerData(rawFile)
return
}
const before = this.beforeUpload(rawFile)
if (before) {
this.readerData(rawFile)
}
},
readerData(rawFile) {
this.loading = true
let rowidx = Number(this.rowidx)
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = e => {
const data = e.target.result
const workbook = XLSX.read(data, { type: 'array' })
const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName]
const header = this.getHeaderRow(worksheet, rowidx)
const results = XLSX.utils.sheet_to_json(worksheet, {range: rowidx})
this.generateData({ header, results })
this.loading = false
resolve()
}
reader.readAsArrayBuffer(rawFile)
})
},
getHeaderRow(sheet, rowidx) {//指定行开始导入
const headers = []
const nameKeyMap = this.nameToKeyMap;
const range = XLSX.utils.decode_range(sheet['!ref'])
let C
const R = rowidx
/* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
/* find the cell in the first row */
let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
if(hdr.indexOf('UNKNOWN ') == -1) {
let key = nameKeyMap[hdr]
let header = {
title:hdr,
key:key
}
headers.push(header)
}
}
return headers
},
isExcel(file) {
return /\.(xlsx|xls)$/.test(file.name)
}
}
}
</script>
<style scoped>
.excel-upload-input{
display: none;
z-index: -9999;
}
.drop{
border: 2px dashed #bbb;
width: 510px;
height: 60px;
line-height: 60px;
margin: 0 auto;
font-size: 24px;
border-radius: 5px;
text-align: center;
color: #bbb;
position: relative;
}
</style>
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 一只新鑫仔
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果