I want to read excel file using angular code. my file is stored somewhere in my local system e.g, C:/data/test.xlsx
I have already tried loading with readFile() & load() methods of exceljs.
they fail to read anything, instead they give error
I found the way to read excel file from Angular code. Convert the file into ArrayBuffer and read it using exceljs
import * as Excel from 'exceljs/dist/exceljs.min.js';
compilerOptions: {
paths": {
"exceljs": [
"node_modules/exceljs/dist/exceljs.min.js"
]
}
}
<input id="uploadBtn" type="file" (change)="readExcel($event)"
class="upload input-common" required />
readExcel(event) {
const workbook = new Excel.Workbook();
const target: DataTransfer = <DataTransfer>(event.target);
if (target.files.length !== 1) {
throw new Error('Cannot use multiple files');
}
/**
* Final Solution For Importing the Excel FILE
*/
const arryBuffer = new Response(target.files[0]).arrayBuffer();
arryBuffer.then(function (data) {
workbook.xlsx.load(data)
.then(function () {
// play with workbook and worksheet now
console.log(workbook);
const worksheet = workbook.getWorksheet(1);
console.log('rowCount: ', worksheet.rowCount);
worksheet.eachRow(function (row, rowNumber) {
console.log('Row: ' + rowNumber + ' Value: ' + row.values);
});
});
});
}
Also polyfill.ts must reference in this sequence ..
import 'exceljs/dist/exceljs.min.js';
import 'zone.js/dist/zone';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With