Parse XLSX with Node and create json

后端 未结 6 825
傲寒
傲寒 2020-12-02 05:05

Ok so I found this really well documented node_module called js-xlsx

Question: How can I parse an xlsx to output json

6条回答
  •  半阙折子戏
    2020-12-02 05:44

    here's angular 5 method version of this with unminified syntax for those who struggling with that y, z, tt in accepted answer. usage: parseXlsx().subscribe((data)=> {...})

    parseXlsx() {
        let self = this;
        return Observable.create(observer => {
            this.http.get('./assets/input.xlsx', { responseType: 'arraybuffer' }).subscribe((data: ArrayBuffer) => {
                const XLSX = require('xlsx');
                let file = new Uint8Array(data);
                let workbook = XLSX.read(file, { type: 'array' });
                let sheetNamesList = workbook.SheetNames;
    
                let allLists = {};
                sheetNamesList.forEach(function (sheetName) {
                    let worksheet = workbook.Sheets[sheetName];
                    let currentWorksheetHeaders: object = {};
                    let data: Array = [];
                    for (let cellName in worksheet) {//cellNames example: !ref,!margins,A1,B1,C1
    
                        //skipping serviceCells !margins,!ref
                        if (cellName[0] === '!') {
                            continue
                        };
    
                        //parse colName, rowNumber, and getting cellValue
                        let numberPosition = self.getCellNumberPosition(cellName);
                        let colName = cellName.substring(0, numberPosition);
                        let rowNumber = parseInt(cellName.substring(numberPosition));
                        let cellValue = worksheet[cellName].w;// .w is XLSX property of parsed worksheet
    
                        //treating '-' cells as empty on Spot Indices worksheet
                        if (cellValue.trim() == "-") {
                            continue;
                        }
    
                        //storing header column names
                        if (rowNumber == 1 && cellValue) {
                            currentWorksheetHeaders[colName] = typeof (cellValue) == "string" ? cellValue.toCamelCase() : cellValue;
                            continue;
                        }
    
                        //creating empty object placeholder to store current row
                        if (!data[rowNumber]) {
                            data[rowNumber] = {}
                        };
    
                        //if header is date - for spot indices headers are dates
                        data[rowNumber][currentWorksheetHeaders[colName]] = cellValue;
    
                    }
    
                    //dropping first two empty rows
                    data.shift();
                    data.shift();
                    allLists[sheetName.toCamelCase()] = data;
                });
    
                this.parsed = allLists;
    
                observer.next(allLists);
                observer.complete();
            })
        });
    }
    

提交回复
热议问题