Vue+ElementUI使⽤SheetJS解析上传的xls或xlsx⽂件表格需求⼀:客户点击上传对应格式的⽣产计划表格⽂件,点击确定后导⼊⽣产计划。
1.下载
在控制台运⾏以下命令
npm install xlsx
2.引⼊
在页⾯引⼊xlsx
3、前端代码
4.JS代码
lodingExcelDate(){
let file =this.$refs["importfile"].files[0];
//获取最后⼀个.的位置
var index = file.name.lastIndexOf(".");//①//获取后缀
var ext = file.name.substr(index +1);
if(ext =="xls"|| ext =="xlsx"){
if(ext =="xls"|| ext =="xlsx"){
let reader =new FileReader();
let that =this;
let mfgorder =[];//excel解析出来的⽣产计划
var data =new Uint8Array(sult);
var workbook = ad(data,{//②
type:'array',
cellText:false,
});
for(let sheet in workbook.Sheets){//③
//获取当前选中的产线
let line = that.lineList.find(item =>{
return item.lineId == that.lineId
})
//判断excel中是否存在对应产线信息
if(!workbook.Sheets.hasOwnProperty(line.lineName)){//④
that.$message({
message:"Excel中⽆对应产线的⽣产计划!",
type:'error'
});
return;
}
//只解析选中的产线的⽣产计划
if(sheet.indexOf(line.lineName)!=-1){//⑤
//解析Excel
mfgorder = XLSX.utils.sheet_to_json(workbook.Sheets[sheet],{//⑥ raw:false,
range:2
})
//处理合并单元格的字段问题设置⽣产订单等字段的键
for(let mfg of mfgorder){//⑦
for(let key in mfg){
switch(key){
case"__EMPTY":
delete mfg[key]
break;
case"__EMPTY_1":
mfg["mfgorderCode"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_2":
js获取json的key和valuemfg["mfgplanCode"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_3":
mfg["mfgorderUser"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_4":
mfg["materialId"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_5":
mfg["mfgorderSmt"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_6":
mfg["mfgorderDate"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_7":
mfg["mfgorderOut"]= mfg[key];
delete mfg[key]
delete mfg[key]
break;
case"__EMPTY_8":
mfg["planQty"]= mfg[key];
delete mfg[key]
break;
case"__EMPTY_9":
delete mfg[key]
break;
}
}
mfg.lineId = that.lineId;
}
console.log(mfgorder)
that.mfgorderList = mfgorder;
break;
}
}
}
}else{
this.$message({
message:"请选择正确的EXCEL⽂件", type:'error'
});
}
},
5.解析结果
附:
后端接取表格数据数据结构:List<Map<String, String>> excelData
⽇期处理代码:DateUtil.date((Long.parseLong(key) - 25569) * 86400 * 1000 - 28800000)①获取后缀代码可以合并为
var ext = file.name.substr(file.name.lastIndexOf(".")+1);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论