I need my app to able to read a prepared excel file (located in my domain (www.***.com/data.xlsx)) into an array. The followings code works like charm for the purpose but its client based where user browse and input for the xlsx file.
import {
Component,
OnInit
} from '@angular/core';
import * as XLSX from 'xlsx';
@Component({
selector: 'app-excelsheet',
templateUrl: './excelsheet.component.html',
styleUrls: ['./excelsheet.component.css']
})
export class ExcelsheetComponent implements OnInit {
data: [][];
constructor() {}
ngOnInit(): void {}
onFileChange(evt: any) {
const target: DataTransfer = <DataTransfer>(evt.target);
if (target.files.length !== 1) throw new Error('Cannot use multiple files');
const reader: FileReader = new FileReader();
reader.onload = (e: any) => {
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, {
type: 'binary'
});
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
console.log(ws);
this.data = (XLSX.utils.sheet_to_json(ws, {
header: 1
}));
console.log(this.data);
let x = this.data.slice(1);
console.log(x);
};
reader.readAsBinaryString(target.files[0]);
}
}
I tried various ways through trial and errors, testing the functions and variables, other libraries, read about client/server based but still now I'm kinda in dead end, maybe because I have less experience into JavaScript.
For Short, How can my app read the excel file(from a URL) to an array everytime the app started?
For extra, I also tried inserting the excel file inside the app directory, but still...
I know I should have put my code attempt for you guys to troubleshoot but I will just think its just a weird attempts as I'm really new. I hope you guys can briefly list what I should use and do, thanks in advance, I appreciate it.