Export data from JSON to excel by preserving data type

1.9k views Asked by At

I am using alasql.js file to export JSON data into excel file. The problem I am facing is that when I export data it is exporting without any problem but the columns which having the type as number are exported as text in excel.

Does any one know any alternate library to export data from json to excel with preserving data type of.

My JSON is look like:--

[{"emplId":"Empl Id","emplName":"Empl Name","formNumber":"Form Number","costCenter":"Cost Center","emplDept":"Empl Dept","invoiceNumber":"Invoice Number","totalPrice":"Total Price","purchaseDate":"Purchase Date","shoesStyle":"Shoes Style","shoesSize":"Shoes Size","shoesWidth":"Shoes Width","companyname":"Companyname","shoePrice":"Shoe Price","taxAmountPaid":"Tax Amount Paid","shippinghandling":"Shippinghandling","typeofPurchase":"Typeof Purchase","storeAccountNumber":"Store Account Number","refundFlag":"Refund Flag","manufacturer":"Manufacturer"},
{"emplId":1234567,"emplName":"Kevin W Hays","formNumber":5734,"costCenter":"PM555","emplDept":"SUPPLY","invoiceNumber":"ACCC1213","totalPrice":201.45,"purchaseDate":"01/05/2015","shoesStyle":"W02053","shoesSize":11,"shoesWidth":"E3","companyname":"XXX","shoePrice":180,"taxAmountPaid":21.45,"shippinghandling":"","typeofPurchase":"Store","storeAccountNumber":"1707401","refundFlag":"N","manufacturer":"XXX"}]

Any help is greatly appreciated.

2

There are 2 answers

0
TJC On

I believe you will have to surround the items you want as strings with double quotes like "text" and leave your numbers alone.

0
Siva On

Convert the JSON string to javascript object and then export it.

var jsonString = "[{\"city\":\"Minsk\",\"population\":100000},{\"city\":\"Riga\",\"population\":200000}]";

var jsonObject = JSON.stringify(jsonString);

alasql("SELECT * INTO XLSX('cities.xlsx',{headers:true}) FROM ? ",[jsonObject]);

If you still cannot solve it post the JSON data and the code you are using to export.