//簡要代碼
download: function() {
this.not_allow_operate = true;
let fileName = `(${new Date().getTime()}).csv`;
let content = []; //\r\n
[...this.$el.querySelectorAll("#scrollBox .exportTR")].forEach((item, index) => {
let tr = []; //,
[...item.querySelectorAll(".exportTD")].forEach((item1, index1) => {
tr.push('\t' + ('value' in item1 ? item1.value : item1.innerText.replace(/,/g, "").trim()));//去除多餘的逗号
});
content.push(tr.join(','));
});
var blob = new Blob(["\ufeff" + content.join('\r\n')], {
type: 'application/vnd.ms-excel'
}); //new Blob(["\ufeff"+content],{ type: 'application/vnd.ms-excel;charset=UTF-8'});"\ufeff"防止Excel打開時亂碼, type 目前發現如果不指定,在ie裡面下載到的檔案是無類型的
var link = document.createElement("a");
link.innerHTML = fileName;
link.download = fileName;
link.href = URL.createObjectURL(blob);
let evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, true);
link.dispatchEvent(evt);
}
loadFile: function() {
var files = this.$el.querySelector('#myfile').files;
if(files.length) {
let that = this;
let modelKey = ['goods_sn', 'product_sn_code', 'goods_name', 'areaname', 'able_number'];
let file = files[0];
let [extension] = file.name.split('.').reverse();
if(/excel+/.test(file.type) && /csv+/.test(extension)) { //判斷檔案類型
var reader = new FileReader(); //new一個FileReader執行個體
reader.onerror = function(data) {
//"檔案加載出錯,請檢查檔案是否存在,並且正確"
};
reader.onload = function() {
let tempList = [];
let offest = 2; //跳過多少條開頭無用數據
let tempArray1 = this.result.split('\r\n');
tempArray1.slice(offest, tempArray1.length).forEach((item, index) => {
let tempArray = item.replace(/\"/g, "").split(",");
if(tempArray.length == modelKey.length) {
let tempOjb = {};
modelKey.forEach((item, index) => {
if(index < tempArray.length) tempOjb[item] = tempArray[index].trim();
});
tempList.push(tempOjb);
}
});
if(tempList.length > 0) {
// "導入成功"
} else {
//"檔案裡沒有符合格式的內容"
}
};
reader.readAsText(file, 'gb2312');//指定讀取編碼,防止中文亂碼
} else {
// "檔案類型錯誤,請選擇CSV檔案"
}
}
}