将 xlsx 转换为 json 时日期格式发生变化
Date format changing when converting xlsx to json
我正在尝试从 xlsx 文件中读取数据并将其转换为 json,但日期列值正在更改
这是屏幕截图:Screenshot of my excel file from which i am reading data
这是从 excel 文件中读取数据并转换为 JSON 的代码:
onBasicUpload(event){
let workBook = null;
let jsonData = null;
const reader = new FileReader();
const file = event.files[0];
console.log(file,"file is here");
reader.onload = (event) => {
const data = reader.result;
workBook = xlsx.read(data, { type: 'binary' });
jsonData = workBook.SheetNames.reduce((initial, name) => {
const sheet = workBook.Sheets[name];
initial[name] = xlsx.utils.sheet_to_json(sheet);
console.log(jsonData,"jsonDAta");
return initial;
}, {});
const dataString = JSON.stringify(jsonData);
console.log(dataString,"stringify data");
this.jsonArr = JSON.parse(dataString)
console.log(this.jsonArr,"parsed json");
console.log(Object.keys(this.jsonArr['data'][0]))
}
reader.readAsBinaryString(file);
}
它返回给我这个:
DOCDT 是返回日期的值。
{“数据”:[{“DOCNO”:“001”,“NETAMOUNT”:30000,“IREF1”:“50”,“IREF2”:“100”,“DOCDT”:43989},{ "DOCNO":2,"NETAMOUNT":40000,"IREF1":40,"IREF2":90,"DOCDT":43989}]}
试试这个,
onBasicUpload(event){
let workBook = null;
let jsonData = null;
const reader = new FileReader();
const file = event.files[0];
console.log(file,"file is here");
reader.onload = (event) => {
const data = reader.result;
workBook = xlsx.read(data, { type: 'binary' , cellDates: true });
jsonData = workBook.SheetNames.reduce((initial, name) => {
const sheet = workBook.Sheets[name];
initial[name] = xlsx.utils.sheet_to_json(sheet);
console.log(jsonData,"jsonDAta");
return initial;
}, {});
const dataString = JSON.stringify(jsonData);
console.log(dataString,"stringify data");
this.jsonArr = JSON.parse(dataString)
console.log(this.jsonArr,"parsed json");
console.log(Object.keys(this.jsonArr['data'][0]))
}
reader.readAsBinaryString(file);
}
日期在 XLSX 文件中存储为数字(参见 here or here)。你得到的就是存储的,所以你得到了号码。
根据第一个参考资料,日期存储到 1900 年 1 月 1 日。您现在需要做的就是拥有一个函数,将此数字转换回日期。纯数学。
幸运的是,一个快速的 SO 搜索显示,这已经完成了:
Converting Excel Date Serial Number to Date using Javascript
我正在尝试从 xlsx 文件中读取数据并将其转换为 json,但日期列值正在更改 这是屏幕截图:Screenshot of my excel file from which i am reading data
这是从 excel 文件中读取数据并转换为 JSON 的代码:
onBasicUpload(event){
let workBook = null;
let jsonData = null;
const reader = new FileReader();
const file = event.files[0];
console.log(file,"file is here");
reader.onload = (event) => {
const data = reader.result;
workBook = xlsx.read(data, { type: 'binary' });
jsonData = workBook.SheetNames.reduce((initial, name) => {
const sheet = workBook.Sheets[name];
initial[name] = xlsx.utils.sheet_to_json(sheet);
console.log(jsonData,"jsonDAta");
return initial;
}, {});
const dataString = JSON.stringify(jsonData);
console.log(dataString,"stringify data");
this.jsonArr = JSON.parse(dataString)
console.log(this.jsonArr,"parsed json");
console.log(Object.keys(this.jsonArr['data'][0]))
}
reader.readAsBinaryString(file);
}
它返回给我这个: DOCDT 是返回日期的值。 {“数据”:[{“DOCNO”:“001”,“NETAMOUNT”:30000,“IREF1”:“50”,“IREF2”:“100”,“DOCDT”:43989},{ "DOCNO":2,"NETAMOUNT":40000,"IREF1":40,"IREF2":90,"DOCDT":43989}]}
试试这个,
onBasicUpload(event){
let workBook = null;
let jsonData = null;
const reader = new FileReader();
const file = event.files[0];
console.log(file,"file is here");
reader.onload = (event) => {
const data = reader.result;
workBook = xlsx.read(data, { type: 'binary' , cellDates: true });
jsonData = workBook.SheetNames.reduce((initial, name) => {
const sheet = workBook.Sheets[name];
initial[name] = xlsx.utils.sheet_to_json(sheet);
console.log(jsonData,"jsonDAta");
return initial;
}, {});
const dataString = JSON.stringify(jsonData);
console.log(dataString,"stringify data");
this.jsonArr = JSON.parse(dataString)
console.log(this.jsonArr,"parsed json");
console.log(Object.keys(this.jsonArr['data'][0]))
}
reader.readAsBinaryString(file);
}
日期在 XLSX 文件中存储为数字(参见 here or here)。你得到的就是存储的,所以你得到了号码。
根据第一个参考资料,日期存储到 1900 年 1 月 1 日。您现在需要做的就是拥有一个函数,将此数字转换回日期。纯数学。
幸运的是,一个快速的 SO 搜索显示,这已经完成了: Converting Excel Date Serial Number to Date using Javascript