从 zip 文件夹读取 excel 文件时未正确解析日期
date is not correctly parsed while reading an excel file from a zip folder
我正在从 ui 上传一个 zip 文件夹,其中有一个 excel 文件。我写了下面的代码
getZipData(file: File) {
const jsZip = require('jszip');
let excelRecord: any[] = [];
let images: { path: string, image: string }[] = [];
let fileList: any[] = [];
const reader = new FileReader();
jsZip.loadAsync(file).then((zip: any) => {
Object.keys(zip.files).forEach((filename) => {
if (!zip.files[filename].dir && filename.includes('xlsx')) {
zip.files[filename].async('ArrayBuffer').then((fileData: any) => {
const data = new Uint8Array(fileData);
const arr = new Array();
for (let i = 0; i != data.length; ++i) {
arr[i] = String.fromCharCode(data[i]);
}
const bstr = arr.join("");
const workbook = XLSX.read(fileData, { type: "binary" });
const first_sheet_name = workbook.SheetNames[0];
const worksheet = workbook.Sheets[first_sheet_name];
excelRecord = XLSX.utils.sheet_to_json(worksheet);
console.log(JSON.stringify(excelRecord));
//{name:"abcd",birthdate:36141} <---- this is the output
});
}
})
})
}
在输出日期 (12-12-1998) 没有被正确解析
它应该是 {name:"abcd",birthdate:"12-12-1998"}
当我手动解析此日期时,它会创建 01/01/1970。我怎样才能得到准确的日期?
我认为你需要将日期乘以 10000000
试试这个
console.log(new Date(36141* 10000000));
更改此行
const workbook = XLSX.read(fileData, { type: "binary"});
到
const workbook = XLSX.read(fileData, { type: "binary", cellDates: true });
您将获得印度标准时间的日期,然后根据您的要求进行转换
示例:
var date = new Date(excelRecord.birthdate).toLocaleDateString();
我正在从 ui 上传一个 zip 文件夹,其中有一个 excel 文件。我写了下面的代码
getZipData(file: File) {
const jsZip = require('jszip');
let excelRecord: any[] = [];
let images: { path: string, image: string }[] = [];
let fileList: any[] = [];
const reader = new FileReader();
jsZip.loadAsync(file).then((zip: any) => {
Object.keys(zip.files).forEach((filename) => {
if (!zip.files[filename].dir && filename.includes('xlsx')) {
zip.files[filename].async('ArrayBuffer').then((fileData: any) => {
const data = new Uint8Array(fileData);
const arr = new Array();
for (let i = 0; i != data.length; ++i) {
arr[i] = String.fromCharCode(data[i]);
}
const bstr = arr.join("");
const workbook = XLSX.read(fileData, { type: "binary" });
const first_sheet_name = workbook.SheetNames[0];
const worksheet = workbook.Sheets[first_sheet_name];
excelRecord = XLSX.utils.sheet_to_json(worksheet);
console.log(JSON.stringify(excelRecord));
//{name:"abcd",birthdate:36141} <---- this is the output
});
}
})
})
}
在输出日期 (12-12-1998) 没有被正确解析 它应该是 {name:"abcd",birthdate:"12-12-1998"} 当我手动解析此日期时,它会创建 01/01/1970。我怎样才能得到准确的日期?
我认为你需要将日期乘以 10000000
试试这个
console.log(new Date(36141* 10000000));
更改此行
const workbook = XLSX.read(fileData, { type: "binary"});
到
const workbook = XLSX.read(fileData, { type: "binary", cellDates: true });
您将获得印度标准时间的日期,然后根据您的要求进行转换
示例:
var date = new Date(excelRecord.birthdate).toLocaleDateString();