import XLSX from 'xlsx'
// 引入之后 下面是转json和excel
_toJson (e) {
let file = e.target.files[0]
let reader = new FileReader()
reader.onload = (event) => {
console.log(event);
let data = event.target.result;
var workbook = XLSX.read(data, {
type: 'binary'
});
let resultArray = []
workbook.SheetNames.forEach(function (sheetName) {
console.log('workbook.Sheets[sheetName]', workbook.Sheets[sheetName]);
const data = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { header: 1 });
console.log('data', data);
let mainData = data.slice(1)
if (mainData.length > 0) {
for (let row = 0; row < mainData.length; row++) {
let rowData = {}
for (let col = 0; col < data[0].length; col++) {
rowData[data[0][col]] = mainData[row][col] || ''
}
resultArray.push(rowData)
}
} else {
alert('至少需要一行有效数据')
}
})
console.log('resultArray', resultArray);
}
reader.readAsBinaryString(file);
},
_toExcel () {
let data = [{ "业务政策": 1, "产品名称": 2, "证件号码": 5 }, { "产品名称": 2 }]
const ws = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "People");
XLSX.writeFile(wb, "sheetjs.xlsx");
}