Node.js/Angular: SheetJS js-xlsx library with issues and fixes.

In this article, we will see how SheetJS js-xlsx library
is used to export  data to xlsx file for a particular use case and
also, I am covering a few issues that I found and fixes for that.

So let's start without wasting time,


Use Case: We need to export a html table (containing all columns and rows) into xlsx file.

Angular Example Link: https://stackblitz.com/edit/angular-xbgicw?file=app%2Fapp.component.ts

I have used the reference of this example in this article.
So please check the Angular Example Link.

Here, I have 2 tables with 3 columns Name, Dob in dd/mm/yyyy format, and
Id which is a string but sometimes its value maybe number 20 digit.

So let's see the happy case:

In this example I have imported xlsx library and has 2 functions importTable1() and importTable2()
which use xlsx util functions to export the tables into xlsx files.

Issues Found:
(Export Table One  button will export xlsx sheet for table1 which has these issues)

1) When you export the sheet for table one using xlsx library the
 Dob column format changes into mm/dd/yyyy if the date value is less or equal to 12.
For example :
Dob for Rubecca in table one is 11/10/1998 and if you export
table one you will see it changed to 10/11/1998.

2) In Id column for table 1 for Zena is a 20 digit number.
  But when you export sheet for table 1 the Id value is round off.
For example Id value '10001000202020101010' is changed to '10001000202020100000'

Fixes:
In the reference example, I have given fixes for these 2 issues.
If you look at table 2, I have used "t='s'" inside <td> tag.
Which considers the respective column as string and does not manipulate value.
So when you click the Export Table Two button it will export xlsx sheet for table2 without any manipulations done to Id and Dob coloumn.

Npm package Link: https://www.npmjs.com/package/xlsx

I hope you like this article. If you have any other solution please let me know in the comments section.

Subscribe this blog for more articles on Node.js and JavaScript.
You can also follow me on Twitter or Linkedin for the latest updates.

Written By:
Saurabh Joshi

Comments

Popular Posts