js-xls - sheet 到 json on Angular returns 只有第一行
js-xls - sheet to json on Angular returns only the first row
我有一个包含此数据的 xlsx 文件:
Totale venduto | Differenza | Totale incassato
3.354,95 | 16,73 | 3.371,68
此文件作为 BLOB
发送到方法 readBlob
,如以下代码所示:
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);
}
}
我不明白为什么当我调用 utils.sheet_to_json
时它 returns 只有第一行数据。
控制台输出如下:
{"!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"]]
我发现了问题。在 documantation 中,我看到工作表默认 range
是 (ws['!ref'])
。
所以查看 console.log(JSON.stringify(ws))
的内容,我发现范围是 "!ref":"A1:C1"
.
设置范围为"!ref":"A1:C2"
:
let xlsData = utils.sheet_to_json(ws, { range: "A1:C2", header: 1 });
输出如下:
[["Totale venduto","Differenza","Totale incassato"],["3,354.95","16.73","3,371.68"]]
我有一个包含此数据的 xlsx 文件:
Totale venduto | Differenza | Totale incassato
3.354,95 | 16,73 | 3.371,68
此文件作为 BLOB
发送到方法 readBlob
,如以下代码所示:
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);
}
}
我不明白为什么当我调用 utils.sheet_to_json
时它 returns 只有第一行数据。
控制台输出如下:
{"!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"]]
我发现了问题。在 documantation 中,我看到工作表默认 range
是 (ws['!ref'])
。
所以查看 console.log(JSON.stringify(ws))
的内容,我发现范围是 "!ref":"A1:C1"
.
设置范围为"!ref":"A1:C2"
:
let xlsData = utils.sheet_to_json(ws, { range: "A1:C2", header: 1 });
输出如下:
[["Totale venduto","Differenza","Totale incassato"],["3,354.95","16.73","3,371.68"]]