选择的文件导入批量转账,导入的是excel文件,
使用的是vue框架+element
安装xlsx cnpm install xlsx
import XLSX from 'xlsx'
使用input的上传文件
<input
type="file"
ref="file"
@change="changeFile"
style="width:96px; padding:7.5px 0;position:relative;opacity:0;z-index:999;"
/>
input函数
changeFile() {
this.tableData = []
var wb; //读取Excel的数据
var excelTab;
var f = this.$refs.file.files[0]
this.fileName = this.$refs.file.files[0].name
var reader = new FileReader();
var fixdata = function (data) { //文件流转BinaryString
var o = "",
l = 0,
w = 10240;
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
return o;
}
reader.onload = (e) => {
var data = e.target.result;
wb = XLSX.read(data, {
type: 'binary'
});
var table = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
for (var i = 0; i < table.length; i++) {
this.tableData.push(table[i])
}
}
reader.readAsBinaryString(f);
},
导入的表格式
导出函数
saveFile() {
var excelName = [['姓名/工号', '地址', '金额', '转账结果']]
for (var i = 0; i < this.tableData.length; i++) {
var temp = []
temp.push(this.tableData[i]["姓名/工号"])
temp.push(this.tableData[i]["地址"])
temp.push(this.tableData[i]["金额"])
temp.push(this.tableData[i]["结果"])
excelName.push(temp)
}
console.log(this.tableData)
var wscols = [{ wch: 15 }, { wch: 50 }, { wch: 15 }, { wch: 70 }]
var xlsxName = '转账记录表'
/* convert state to workbook */
const ws = XLSX.utils.aoa_to_sheet(excelName)
ws['!cols'] = wscols
const wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, ws, xlsxName)
/* generate file and send to client */
XLSX.writeFile(wb, xlsxName + ".xlsx")
},
导出结果
批量转账采用了同步函数,根据前一个转账结果来判断下一个是否转账