简体   繁体   中英

How to convert cell type number to string in XLSX? For Handling Date format

I'm using npm i xlsx for reading xlsx file. so my questions here ... in my xlsx sheet i given Indian standard date (DD/MM/YYYY) like 24/09/2020 so it's giving same value... if I give 03/09/2020 it's giving number 43898 like this. set cellDates:true it's giving 2020-03-09T00:00:00.000Z so here date take as a month .. I don't want like this .i want same string 03 /09/2020.. my code is here

var sheet_name_list = workbook.SheetNames; 
var wSheet = workbook.Sheets[sheet_name_list[0]]; 
let xlsxDate = XLSX.utils.sheet_to_json(wSheet, { "defval": null, raw: true });
 xlsxDate.forEach(function (val, index) {
       console.log(val)
 })

Excel's date format is a bit different. When you write a date value like "08.09.2020" it will parse it to a number (like 43898). When you write a date value to Excel, it will calculate how many days passed from 01.01.1900 to the given date.

So the number you get (43898) is just the number of days passed between 01.01.1900 and 09.09.2020.

Edit your sheet_to_json method like this:

XLSX.utils.sheet_to_json(wSheet,{"defval": null, raw:true, dateNF "dd/mm/yyyy"})

You basically need to add dateNF : "dd/mm/yyyy" to your options object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM