Writing special characters to excel file using xlsx-populate

1.1k Views Asked by At

I am using xlsx-populate to write to excel files. Whenever the text contains special characters such as smart quotes or bullets points, it gets printed as weird character like ’. However if replace them with their Unicode value (Right Single Quotation mark with \u2019), it renders correctly in excel sheet.

I tried js libraries like slugify to convert the special characters into ascii version but all of them would individually convert every constituent byte of special char and not as whole.

Currently, I replace the special character with their Unicode value using regex but there will be always some character that I will miss. Is there a better way to handle this problem?

1

There are 1 best solutions below

0
LoneWolf On

I used iconv-lite library to first encode the string into 'win1252' and then decoded it with 'utf-8'. The resulting string renders correctly when written to excel.

iconv.decode(iconv.encode(string, 'win1252'), 'utf-8')

Thanks @JosefZ for suggesting this path.