Open In App

How to Convert CSV to Excel in Node.js ?

NodeJS has become one of the famous backend frameworks for development. So in this article, we’ll see one of its use to convert CSV into Excel

We will use CSVtoExcel npm package to do the conversion of files. It provides convertCsvToXlsx function to implement the conversion.



convertCsvToXlsx(source, destination);

Steps to Implement:

Modules Required:



 

Creating Nodejs Application And Installing Module:

Project Structure: It will look like the following.

Report.csv File

Code:




// Importing modules
const path = require('path');
const convertCsvToXlsx = require('@aternus/csv-to-xlsx');
  
// Specifying source directory + file name
let source = path.join(__dirname, 'report.csv');
  
// Specifying destination directory + file name
let destination = path.join(__dirname, 'converted_report.xlsx');
  
// try-catch block for handling exceptions
try {
  
    // Functions to convert csv to excel
    convertCsvToXlsx(source, destination);
} catch (e) {
  
    // Handling error
    console.error(e.toString());
}

Output:

Excel File:

Article Tags :