console
new Vue({
el: "#app",
data: {
res: {}
},
methods: {
display() {
const fileList = this.$refs.file.files;
const { fixdata, res, sheet2blob, openDownloadXLSXDialog } = this;
for (let i = 0; i < fileList.length; i++) {
const fr = new FileReader();
fr.onload = ({ target: { result } }) => {
const workbook = XLSX.read(result, {
type: 'binary'
})
if (workbook) {
for (let sheet in workbook.Sheets) {
if (workbook.Sheets.hasOwnProperty(sheet)) {
const s = workbook.Sheets[sheet];
for (let key in s) {
if (key['0'] === 'A' && key !== 'A1') {
const tmp = 'B' + key.replace('A', '');
this.res[s[key].v] = s[tmp].v + (res[s[key].v] || 0);
}
}
}
}
}
}
fr.readAsBinaryString(fileList[i]);
}
setTimeout(() => {
openDownloadXLSXDialog(sheet2blob(Object.keys(res).map(k => {
return {
"品种编号": k,
"数量小计": res[k]
}
})), '统计.xlsx');
}, 1000);
},
sheet2blob(data, sheetName) {
sheetName = sheetName || 'sheet1';
var filename = "统计.xlsx";
const wb = XLSX.utils.book_new(), ws = XLSX.utils.json_to_sheet(data);
XLSX.utils.book_append_sheet(wb, ws, "统计");
const wbout = XLSX.writeFile(wb, filename);
return new Blob([wbout], { type: "application/octet-stream" });
},
openDownloadXLSXDialog(url, saveName) {
if (typeof url == 'object' && url instanceof Blob) {
url = URL.createObjectURL(url);
}
const aLink = document.createElement('a');
aLink.href = url;
aLink.download = saveName || '';
let event;
if (window.MouseEvent) event = new MouseEvent('click');
else {
event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
aLink.dispatchEvent(event);
}
}
})
<div id="app">
<input type="file" @change="display" ref="file" multiple="multiple">
</div>