I have an xlsx file with this data:
Totale venduto | Differenza | Totale incassato
3.354,95 | 16,73 | 3.371,68
This file is sended to a method readBlob
as a BLOB
, like in the following code:
import { utils, write, read, readFile, WorkBook, WorkSheet, } from 'xlsx';
export class ExportXlsService
{
public readBlob(data: any)
{
let reader = new FileReader()
reader.onload = () =>
{
let u8 = new Uint8Array(reader.result);
let wb: WorkBook = read(u8, { type: 'array' });
let wsname: string = wb.SheetNames[0];
let ws: WorkSheet = wb.Sheets[wsname];
console.log(JSON.stringify(ws));
// Read data
let xlsData = utils.sheet_to_json(ws, { header: 1 });
console.log(JSON.stringify(xlsData));
}
reader.readAsArrayBuffer(data);
}
}
I don't understand why when I call utils.sheet_to_json
it returns only the first data row.
The following is the console output:
{"!ref":"A1:C1","A1":{"t":"s","v":"Totale venduto","r":"<t>Totale venduto</t>","h":"Totale venduto","w":"Totale venduto"},"B1":{"t":"s","v":"Differenza","r":"<t>Differenza</t>","h":"Differenza","w":"Differenza"},"C1":{"t":"s","v":"Totale incassato","r":"<t>Totale incassato</t>","h":"Totale incassato","w":"Totale incassato"},"A2":{"t":"n","v":3354.95,"w":"3,354.95"},"B2":{"t":"n","v":16.72999945282936,"w":"16.73"},"C2":{"t":"n","v":3371.679999452829,"w":"3,371.68"}}
[["Totale venduto","Differenza","Totale incassato"]]
I found the problem. In the documantation I see that the worksheet default range
is the (ws['!ref'])
.
So lookin the content of console.log(JSON.stringify(ws))
I see that the range was "!ref":"A1:C1"
.
Setting the range as "!ref":"A1:C2"
:
let xlsData = utils.sheet_to_json(ws, { range: "A1:C2", header: 1 });
the output is the following:
[["Totale venduto","Differenza","Totale incassato"],["3,354.95","16.73","3,371.68"]]