node package manager
Orgs are free. Discover, share, and reuse code in your team. Create a free org ยป

excelcsv

ExcelCSV

Parse an excel workbook (.xlsx), and all its worksheets, into a single CSV file, and transform the output.

I wrote ExcelCSV to solve this problem:

A client needed some black magic to transform a massive excel workbook, with hundreds of worksheets, into a single CSV, to eventually import the CSV into a database. Messy, I know. Unfortunately, excel does not have native support for exporting a collection of worksheets as a CSV. There are plenty of other excel parsers out there, but most of them only help you read data, when I needed to both read and transform my data.

Installation

npm install excelcsv

or

git clone https://github.com/Moonfuse/excelcsv.git
cd excelcsv
npm install

Features

  • Preprocess each row
    • Transform data
    • Ignore data
  • Append a header

Example

var ExcelCSV = require('excelcsv');

var header  = ['id', 'email']
  , fileIn  = 'src/example.xlsx'
  , fileOut = 'dist/example.csv';

// fileOut is optional.
var parser = new ExcelCSV( fileIn, fileOut );
var csv = parser
            // optional.
            .header( header )
            // optional.
            .row(function(worksheet, row) {
              // Transform data here or return false to skip the row.
              return row;
            })
            .init();

Methods

init()

Runs the script and returns the CSV as a string.

header( Array )

[optional] Append a header to the beginning of the CSV.

row( callback(Array row, String worksheetName) )

[optional] Preprocess your data.

The callback accepts two parameters:

  • row (Array)
  • worksheetName (String)

Row values are not modified by reference, so you must return an array w/ the desired output for each row, or false to skip the row altogether.

worksheetName can be useful for adding extra data to your rows

Appendix

Approaches

It should be known, that there are other ways to solve this problem.

  • If your're a VB guy, you can write a excel macro and export your data as a XML file, and perhaps a CSV.
  • Excel for Windows, has functionality that allows you to attach an XML schema, map cells to said schema, and eventually export and XML file.

Maintenance

Feedback is most welcome; if you want to help make improvements, fork and pull, file an issue, or shoot jay@moonfuse.co an email.