js read excel file code example
Example 1: exceljs read file example
<input type="file" @change="handleChange" />
handleChange(e) {
this.file = e.target.files[0]
},
handleImport() {
const wb = new Excel.Workbook();
const reader = new FileReader()
reader.readAsArrayBuffer(this.file)
reader.onload = () => {
const buffer = reader.result;
wb.xlsx.load(buffer).then(workbook => {
console.log(workbook, 'workbook instance')
workbook.eachSheet((sheet, id) => {
sheet.eachRow((row, rowIndex) => {
console.log(row.values, rowIndex)
})
})
})
}
}
Example 2: read excel file using javascript html
var XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
function handleFile(e) {
var files = e.target.files, f = files[0];
var reader = new FileReader();
reader.onload = function(e) {
var data = new Uint8Array(e.target.result);
var workbook = XLSX.read(data, {type: 'array'});
};
reader.readAsArrayBuffer(f);
}
input_dom_element.addEventListener('change', handleFile, false);
Example 3: exceljs read file
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(filename)
.then(function() {
var worksheet = workbook.getWorksheet(sheet);
worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
});
});