Angular 4: How to read data from Excel?

前端 未结 5 2002
孤城傲影
孤城傲影 2020-12-01 11:09

I am not able to get data from an Excel sheet in Angular 4. Below is my code sample.

HTML code:



        
5条回答
  •  庸人自扰
    2020-12-01 11:32

        readfile() {
        // You can change the file path in the assets folder
        let url = "/assets/template.xlsx";
        let req = new XMLHttpRequest();
        req.open("GET", url, true);
        req.responseType = "arraybuffer";
        req.onload =  (e) => {
            let data = new Uint8Array(req.response);
            let workbook = XLSX.read(data, {type: "array"});
            const excelBuffer: any = XLSX.write(workbook, {bookType: 'xlsx', type: 'array'});
            // TO export the excel file
            this.saveAsExcelFile(excelBuffer, 'X');
        };
        req.send();
    }
    

    You can take this code and change the path to your file; for the last line is for export it. To know more about it i suggest to visit the official doc of js-xlsx https://github.com/SheetJS/js-xlsx

提交回复
热议问题