Introduction
Excel XLSX to CSV file converter. Node.js library and command line utility. Lightweight.
Getting Started
Install the package:
git clone https://github.com/papnkukn/xlsx2csv && cd xlsx2csv npm install -g .
Convert the document:
xlsx2csv --verbose --sheet Sample path/to/sample.xlsx sample.csv
Command Line
Usage:
xlsx2csv [options] <input.xlsx> <output.csv>
Options:
--force Force overwrite file
--data [type] Data type to export: formula, value or display
--sheet [name] Sheet name to export
--range [A1:C3] Range to export
--separator [char] CSV column separator, e.g. 't'
--line-end [char] End of line char(s), e.g. 'rn'
--help Print this message
--verbose Enable detailed logging
--version Print version number
Examples:
xlsx2csv --version
xlsx2csv --verbose --range A1:M30 --separator , file.xlsx
xlsx2csv --data formula --sheet Sample file.xlsx file.csv
Using as library
var xlsx2csv = require('node-xlsx2csv'); var options = { verbose: true, sheet: "Sample" }; xlsx2csv('path/to/sample.xlsx', options, function(error, result) { if (error) return console.error(error); console.log(result); });
Options
Name | Type | Description |
---|---|---|
verbose | bool | Detailed output while processing |
data | string | Type of data to export: «formula» to export cell formula, «value» to prefer cell value, or «display» for formatted value |
sheet | string | required, worksheet name to export, e.g. «Sample» |
range | string | optional, capture cell range, e.g. «A1:M30» |
separator | string | CSV column separator, e.g. «,» or «;» or «t», default: «,» |
lineEnd | string | end of line char(s), e.g. «rn» or «r» or «n», default: «n» |
I have a client-side web application, with a very minimal node server to access some data that the client can’t. One of these things is excel spreadsheets with .xls extensions.
I’m trying to get my server set up to download the xls, convert it to csv, and then send it back to the client. I’ve got the download part done, and I’m sure I can figure out the «send back» part, but I can’t for the life of me find a good library to convert from xls to csv.
Can someone point me to a library that can do that in a simple fashion? The excel file is just one sheet, no complicated workbooks or anything.
Or is there another way of doing this I’m not thinking of?
asked Dec 17, 2015 at 19:11
fnsjdnfksjdbfnsjdnfksjdb
1,6335 gold badges19 silver badges33 bronze badges
I am using this package to convert XLSX to CSV:
https://www.npmjs.com/package/xlsx
XLSX = require('xlsx');
const workBook = XLSX.readFile(inputFilename);
XLSX.writeFile(workBook, outputFilename, { bookType: "csv" });
answered Mar 9, 2019 at 22:32
2
There is no library that I am aware of, but you could use node-xlsx to parse the excel file, get the rows and make the CSV yourself. Here’s an example:
var xlsx = require('node-xlsx');
var fs = require('fs');
var obj = xlsx.parse(__dirname + '/test.xls'); // parses a file
var rows = [];
var writeStr = "";
//looping through all sheets
for(var i = 0; i < obj.length; i++)
{
var sheet = obj[i];
//loop through all rows in the sheet
for(var j = 0; j < sheet['data'].length; j++)
{
//add the row to the rows array
rows.push(sheet['data'][j]);
}
}
//creates the csv string to write it to a file
for(var i = 0; i < rows.length; i++)
{
writeStr += rows[i].join(",") + "n";
}
//writes to a file, but you will presumably send the csv as a
//response instead
fs.writeFile(__dirname + "/test.csv", writeStr, function(err) {
if(err) {
return console.log(err);
}
console.log("test.csv was saved in the current directory!");
});
answered Dec 17, 2015 at 19:44
heinstheinst
8,4207 gold badges40 silver badges77 bronze badges
6
In this article, we gonna learn how to import and export excel and csv in node.js.
1. Let’s create a new express project using express generator.
npm i -g express-generator
express node-excel-csv --view=hbs
2. Create a folder config and inside this create a file database.js.
config/database.js
module.exports = {
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD
}
3. Install dotenv npm package.
npm i dotenv
After Installation import dotenv in app.js
require('dotenv').config();
4. Create a .env file in the root and add these environment variables.
DB_HOST=localhost // your database host
DB_DATABASE=node-excel-csv // your database name
DB_USERNAME=root // your database username
DB_PASSWORD= // your database password
5. Install sequelize and mysql2 npm packages.
npm i sequelize mysql2
6. Create a models folder and inside this create a connection.js and movie.model.js
models/connection.js
const { Sequelize } = require('sequelize');
const config = require('../config/database');
const sequelize = new Sequelize(config.database, config.username, config.password, {
host: config.host,
dialect: 'mysql',
operatorsAliases: 'false',
logging: false
});
module.exports = sequelize
models/movie.model.js
const { DataTypes } = require('sequelize');
const sequelize = require('./connection');
const Movie = sequelize.define('Movie', {
movie: {
type: DataTypes.STRING
},
category: {
type: DataTypes.STRING
},
director: {
type: DataTypes.STRING
},
rating: {
type: DataTypes.FLOAT(8, 1)
}
}, {
underscored: true
});
module.exports = Movie;
Note:- Don’t forget to create a movies table. https://github.com/ultimateakash/node-excel-csv/blob/master/node-excel-csv.sql
Above Schema Based on the import(excel or csv) file.
7. Install multer and uniqid npm packages.
npm i multer, uniqid
8. create a folder helpers and inside this folder create a file uploader.js
const multer = require('multer');
const uniqid = require('uniqid');
const path = require('path');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'storage/uploads/')
},
filename: function(req, file, cb) {
cb(null, Date.now() + uniqid() + path.extname(file.originalname))
}
})
const upload = multer({ storage: storage });
exports.upload = (field) => upload.single(field);
9. Create a storage folder and inside this create uploads and outputs folder.
10. Install xlsx npm package.
npm i xlsx
xlsx package provides a bunch of functions for reading and writing CSV/Excel files.
Parsing functions:-
XLSX.read(data, read_opts) attempts to parse data
XLSX.readFile(filename, read_opts) attempts to read filename and parse.
Note:- you can pass raw option to false if you want formatted data.(example — formatted date)
XLSX.read(data, { raw: false })
XLSX.readFile(filename, { raw: false })
Writing functions:-
XLSX.write(wb, write_opts) attempts to write the workbook
XLSX.writeFile(wb, filename, write_opts) attempts to write workbook
Utility Functions:-
Constructing:-
book_new creates an empty workbook
book_append_sheet adds a worksheet to a workbook
Importing:
aoa_to_sheet converts an array of arrays of JS data to a worksheet
json_to_sheet converts an array of JS objects to a worksheet
sheet_add_aoa adds an array of arrays of JS data to an existing worksheet.
sheet_add_json adds an array of JS objects to an existing worksheet.
Exporting:
sheet_to_json converts a worksheet object to an array of JSON objects.
sheet_to_csv generates delimiter-separated-values output.
Ref:- https://www.npmjs.com/package/xlsx
11. Create a folder controllers and inside this folder create movie.controller.js
controllers/movie.controller.js
const XLSX = require("xlsx");
const Movie = require("../models/movie.model")
const outputPath = 'storage/outputs'
exports.index = async (req, res) => {
const movies = await Movie.findAll();
return res.render('index', { movies });
}
exports.import = async (req, res) => {
const wb = XLSX.readFile(req.file.path);
const sheets = wb.SheetNames;
if(sheets.length > 0) {
const data = XLSX.utils.sheet_to_json(wb.Sheets[sheets[0]]);
const movies = data.map(row => ({
movie: row['Movie'],
category: row['Category'],
director: row['Director'],
rating: row['Rating']
}))
await Movie.bulkCreate(movies);
}
return res.redirect('/');
}
exports.export = async (req, res) => {
const movies = await Movie.findAll({
attributes: [
'id',
'movie',
'category',
'director',
'rating'
],
raw: true
});
const headings = [
['Id', 'Movie', 'Category', 'Director', 'Rating']
];
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(movies, {
origin: 'A2',
skipHeader: true
});
XLSX.utils.sheet_add_aoa(ws, headings);
XLSX.utils.book_append_sheet(wb, ws, 'Movies');
const buffer = XLSX.write(wb, { bookType: 'csv', type: 'buffer' });
res.attachment('movies.csv');
return res.send(buffer);
}
Note:- In this article, I am using import file headers if your file doesn’t have a headers row then you can use indexes.
You need to pass header option with sheet_to_json
const data = XLSX.utils.sheet_to_json(wb.Sheets[sheets[0]], { header: 1 });
const movies = data.map(row => ({
movie: row[0],
category: row[1],
director: row[2],
rating: row[3]
}))
In the above code you can change the extension based on the file you want to import or export. (.xlsx, .xls, .csv)
Note:- If want to store your exported file on the server you can use writeFile function.
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(movies, {
origin: 'A2',
skipHeader: true
});
XLSX.utils.sheet_add_aoa(ws, headings);
XLSX.utils.book_append_sheet(wb, ws, 'Movies');
const filepath = `${outputPath}/movies.csv`;
XLSX.writeFile(wb, filepath);
return res.download(filepath)
12. Create routes.
routes/index.js
const express = require('express');
const router = express.Router();
const movieController = require('../controllers/movie.controller');
const { upload } = require('../helpers/uploader');
router.get('/', movieController.index);
router.post('/import', upload('file'), movieController.import);
router.get('/export', movieController.export);
module.exports = router;
13. Open views/index.hbs and add the following code.
<main>
<div class="row mb-2">
<div class="col-sm-8 offset-2">
<div class="row">
<div class="col-md-6">
<form method="POST" action="/import" enctype="multipart/form-data">
<div class="input-group">
<div class="custom-file">
<input type="file" name="file" class="custom-file-input" id="inputGroupFile" required accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
<label class="custom-file-label" for="inputGroupFile">Choose file</label>
</div>
<div class="input-group-append">
<button type="submit" class="btn btn-primary float-right mr-2">Import <i class="fa fa-upload"></i></button>
</div>
</div>
</form>
</div>
<div class="col-md-6">
<a href="/export" class="btn btn-primary float-right {{#unless movies.length}} disabled {{/unless }}" role="button" aria-disabled="true">
Export <i class="fa fa-download"></i>
</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-8 offset-2">
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Movie</th>
<th scope="col">Category</th>
<th scope="col">Director</th>
<th scope="col">Rating</th>
</tr>
</thead>
<tbody>
{{#each movies}}
<tr>
<th scope="row">{{ this.id }}</th>
<td>{{ this.movie }}</td>
<td>{{ this.category }}</td>
<td>{{ this.director }}</td>
<td><span class="badge bg-warning text-dark">{{ this.rating }}</span></td>
</tr>
{{else}}
<tr>
<td colspan="5" class="text-center">No Movies Found.</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</div>
</main>
Note:- please checkout github repo — https://github.com/ultimateakash/node-excel-csv
14. Finally start the project.
npm start
Open http://localhost:3000 and try import and export.
Extras:- if you want to read a file without uploading then you can use multer.memoryStorage()
const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });
exports.upload = (field) => upload.single(field);
with memoryStorage() you can access the file buffer.
const wb = XLSX.read(req.file.buffer);
const sheets = wb.SheetNames;
Sample Excel CSV Files:- https://github.com/ultimateakash/node-excel-csv/tree/master/sample%20files
Checkout my full node-excel-csv example. https://github.com/ultimateakash/node-excel-csv
If you facing any issues. don’t hesitate to comment below. I will be happy to help you.
Thanks.
excel2csv
Convert Excel files to CSV
Installation
To install locally:
npm install excel2csv
To install globally:
npm install -g excel2csv
Standalone Use Case
To use as a standalone application, install globally. To get help
excel2csv -h
To convert a xlsx
or xls
file to csv
:
excel2csv -o output.csv input.xlsx
If output filename is not provided via -o
or --output
option, the same filename as the input will be used with .csv
extension.
The sheet to be converted can be provided either using a 0-based index or its name. To provide the sheet index, -n
or --sheet-index
can be used followed by an index. If not present, index 0 will be used. To provide a sheet name, -s
or --sheet-name
options can be used followed by a sheet name. If either the sheet index or name are invalid, the first sheet will be converted by default. If both sheet index and name options are present, the sheet name will be ignored.
Local Use Case
To use locally, install the package in the local directory. Then, the package can be imported as:
const excel2csv = require('excel2csv');
The package includes convert
function with the following arguments:
excel2csv.convert(excelPath, options);
excelPath
is a string path to the input Excel file. options
object is optional and has the following format:
options = {
csvPath, // string path to the output CSV file
sheetIndex, // optional, 0-based index of the Excel sheet to be converted to CSV (default is 0)
sheetName, // optional, sheet name in the Excel file to be converted to CSV
writeCsv, // if true, the output will be written to a file, otherwise will be returned by the function
}
In the options
, any invalid sheetIndex
or sheetName
results into the first sheet being converted. If both sheetIndex
and sheetName
are present, sheetName
is ignored.
The convert
function returns a promise. If the writeCsv
option is true
, the function returns a boolean promise, which is true
if file is successfully written, and false
otherwise. If writeCsv
is set to false
(default value), the convert
function returns a string promise containing the CSV output.
Weekly Downloads (14)
- GitHub Stars
-
0
- Forks
-
2
- Contributors
-
1
Direct Usage Popularity
The npm package excel2csv receives a total of
14 downloads a week. As such, we scored
excel2csv popularity level to be Limited.
Based on project statistics from the GitHub repository for the
npm package excel2csv, we found that it has been
starred ? times.
Downloads are calculated as moving averages for a period of the last 12
months, excluding weekends and known missing data points.
Commit Frequency
- Open Issues
- 0
- Open PR
- 0
- Last Release
-
4 years ago
- Last Commit
-
4 years ago
Further analysis of the maintenance status of excel2csv based on
released npm versions cadence, the repository activity,
and other data points determined that its maintenance is
Inactive.
An important project maintenance signal to consider for excel2csv is
that it
hasn’t seen any new versions released to npm in the
past 12 months, and could be considered as a discontinued project, or that which
receives low attention from its maintainers.
In the past month we didn’t find any pull request activity or change in
issues status has been detected for the GitHub repository.
- Node.js Compatibility
-
not defined
- Age
-
5 years
- Dependencies
-
2 Direct
- Versions
-
6
- Install Size
-
152 kB
- Dist-tags
-
1
- # of Files
-
14
- Maintainers
-
1
- TS Typings
-
Yes
excel2csv has more than a single and default latest tag published for
the npm package. This means, there may be other tags available for this
package, such as next to indicate future releases, or stable to indicate
stable releases.