效果演示
前端代码
<template>
<div>
<div>
<p>模型或者输入项</p>
<vue-json-editor
v-model="jsontarget"
:show-btns="false"
:mode="'code'"
lang="zh"
@json-change="onJsonChange"
@json-save="onJsonSave"
@has-error="onError"></vue-json-editor>
</div>
<p/>
<div class="insuranceDataImport">
<el-upload
class="upload-demo"
action=""
:on-change="handleChange"
:on-remove="handleRemove"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
:auto-upload="false">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只 能 上 传 xlsx / xls 文 件</div>
</el-upload>
</div>
<template>
<el-table
:data="tableData" border height="680px" style="width: 100%" :row-class-name="tableRowClassName">
<el-table-column
prop="KEY"
label="KEY"
width="180">
</el-table-column>
<el-table-column
prop="VALUE"
label="VALUE"
width="180">
</el-table-column>
<el-table-column
prop="A_Exsits"
label="key是否存在">
</el-table-column>
<el-table-column
prop="value_Exsits"
label="value是否存在">
</el-table-column>
<el-table-column
prop="B_Exsits"
label="key对应VALUE值">
</el-table-column>
</el-table-column>
</el-table>
</template>
</div>
</template>
<script>
import vueJsonEditor from 'vue-json-editor'
import XLSX from "xlsx"
import axios from 'axios'
import { platformBaseUrl } from '@/api/baseHost'
import tp1 from '@/components/CardTemplate/tp1.vue'
export default {
components: {
vueJsonEditor
},
data () {
return {
jsontarget:{},
tableData: [{
'KEY': 'demo',
'VALUE': 'msg',
'A_Exsits': 'Y',
'value_Exsits':'demo of jsoneditor',
'B_Exsits':'Y',
},{
'KEY': 'demo1',
'VALUE': 'msg1',
'A_Exsits': 'Y',
'value_Exsits':'demo of jsoneditor1',
'B_Exsits':'Y',
}],
fileTemp:null,
}
},
methods: {
tableRowClassName(tableData) {
// debugger
if (JSON.parse(JSON.stringify(tableData)).row.A_Exsits == "N" || JSON.parse(JSON.stringify(tableData)).row.B_Exsits=="N" ||JSON.parse(JSON.stringify(tableData)).row.value_Exsits==-999)
{
return 'success-row';
}
return '';
},
onJsonChange (value)
{
console.log('value:', value);
},
showModal(msg) {
console.log('showModal resTrans msg.data after', msg)},
onJsonSave (value) {
console.log('value:', value);
},
onError (value) {
console.log('value:', value);
},
handleChange(file, fileList){
this.fileTemp = file.raw
},
handleRemove(file,fileList){
this.fileTemp = null
},
handleChange(file, fileList) {
this.fileTemp = file.raw
// 判断上传文件格式
if (this.fileTemp) {
if ((this.fileTemp.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') || (this.fileTemp.type ==
'application/vnd.ms-excel')) {
this.importfxx(this.fileTemp)
} else {
this.$message({
type: 'warning',
message: '附件格式错误,请删除后重新上传!'
})
}
} else {
this.$message({
type: 'warning',
message: '请上传附件!'
})
}
},
// 移除excel表
handleRemove(file, fileList) {
this.fileTemp = null
},
importfxx(obj) {
let _this = this;
// 通过DOM取文件数据
this.file = obj
var rABS = false; //是否将文件读取为二进制字符串
var f = this.file;
var jsontarget=this.jsontarget
// var tableData=this.tableData
var reader = new FileReader();
FileReader.prototype.readAsBinaryString = function(f) {
var binary = "";
var rABS = false; //是否将文件读取为二进制字符串
// var pt = this;
var wb; //读取完成的数据
var outdata;
var reader = new FileReader();
/*
在读取后,触发的事件,并获取读取文件的地址
*/
reader.onload = function(e) {
//返回的结果为reader.result是类型化数组
var bytes = new Uint8Array(reader.result);
var length = bytes.byteLength;
for(var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]); //将 Unicode 编码转为一个字符:
}
var XLSX = require('xlsx');
if(rABS) {
wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
type: 'base64'
});
} else {
wb = XLSX.read(binary, {
type: 'binary'
});
}
outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);//outdata就是你想要的东西
this.da = [...outdata]
console.log('入参:'+this.da+'-----'+jsontarget)
axios.post(platformBaseUrl + "/api/modeCheck/", {jsonA:this.da,jsonB:jsontarget})
.then(response => {
// debugger
_this.tableData = response.data.result;
console.log('成功了'+_this.tableData)
return _this.tableData
})
.catch(err => {
// this.tableData =response.data.result;
console.log('失败了')
});
// return outdata
}
//开始读取指定的Blob中的内容,一旦完成,result属性中保存的将是被读取文件的
reader.readAsArrayBuffer(f);
}
if(rABS) {
reader.readAsArrayBuffer(f);
} else {
reader.readAsBinaryString(f);
}
},
}
}
</script>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #1E90FF;
}
</style>