Converting xlsx file to txt is working! But i have an issue with special characte

31 Views Asked by At

I have a code that converts xlsx or csv files to txt files, and it works fine!

However, there is a little issue. When i want it to convert files with special characters like letters in russian, japanese or things such as this, the special characters are replaced with question marks. I want to know how can i go about it in a way that the special characters will still be converted to txt file.

Thank you!

The code is as follows. I dont know what i should do to make it work.

const fs = require('fs');
const xlsx = require('xlsx');

const xlsxFilePath = process.argv[2];
const txtFilePath = process.argv[3];

const workbook = xlsx.readFile(xlsxFilePath);
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];

// Convert worksheet to CSV data
const txtData = xlsx.utils.sheet_to_csv(worksheet);

// Remove commas from the txtData
const txtDataWithoutCommas = txtData.replace(/,/g, '');

// Remove lines containing only whitespace
const txtDataWithoutEmptyLines = txtDataWithoutCommas.replace(/^\s*[\r\n]/gm, '');

// Write the processed data to the txt file
fs.writeFileSync(txtFilePath, txtDataWithoutEmptyLines, 'utf-8');
0

There are 0 best solutions below