Exceljs: Read from a csv file

Created on 26 Dec 2019  路  4Comments  路  Source: exceljs/exceljs

I have a csv file uploaded in input type file and I want to read this file the exceljs but I don't know how ? can you please help me with that.
In the documentation it's not mention how to read before prepossessing with exceljs this are the examples in the documentation :

// read from a file
var workbook = new Excel.Workbook();
workbook.csv.readFile(filename)
    .then(function(worksheet) {
        // use workbook or worksheet
    });

// read from a stream
var workbook = new Excel.Workbook();
workbook.csv.read(stream)
    .then(function(worksheet) {
        // use workbook or worksheet
    });

// pipe from stream
var workbook = new Excel.Workbook();
stream.pipe(workbook.csv.createInputStream());

This the begining of my code :

function ValidateFile(input) {

    var files = input.files || [];
    if (!files.length) return;
    var file = files[0];

    fileName = file.name;

   //what to do here ???

   var workbook = new Excel.Workbook();
   workbook.csv.read(stream)
    .then(function(worksheet) {

    });
}

Wich one can I use and how ?
Thank you.

help wanted

Most helpful comment

Is there a chance to read csv and xlsx in browser?

All 4 comments

Are you using it in a NodeJs environment?

If so, ensure the csv file is uploaded to your server, you can use multer npm package to achieve that if getting it from your client.

so let's say you have uploaded the csv file into your server in a folder called upload

You can read the file like this

const Excel = require('exceljs')
const filename = 'upload/yourfile.csv'
const workbook =  new Excel.Workbook()

workbook.csv.readFile(filename)
  .then(worksheet => {
    console.log(worksheet.getCell('A1').value)
  });

What the code does is to read your csv file and return the sheet1 as worksheet, using .then() promise syntax we use the getCell method on the worksheet to get the value from cell A1 in your csv and print to the console.

import fs from 'fs' import { Duplex } from 'stream'`

function bufferToStream (buffer) {
let stream = new Duplex()
stream.push(buffer)
stream.push(null)
return stream
}

fs.readFile(filepath, (err, buffer) => {
if (err) {
console.log(err)
} else {
const stream = bufferToStream (buffer)
workbook.csv.read(stream)
.then(function(worksheet) {
...
}).catch(err => {
console.log(err)
})
}
})

Is there a chance to read csv and xlsx in browser?

This is really frustrating. Xlsx works really well in browser via the load method if you load an ArrayBuffer via FileReader, but load is inexplicably missing for CSVs. I can't imagine why, a corresponding writeBuffer method exists, why not load??

The need to load a csv file is always present on browser, even if it is slower or less efficient. It makes absolutely no sense why CSV requires a node environment but Xlsx does not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcrystalj picture dcrystalj  路  3Comments

abmj1979 picture abmj1979  路  3Comments

TranBaVinhSon picture TranBaVinhSon  路  4Comments

piyushmahensaria picture piyushmahensaria  路  3Comments

lukelaws picture lukelaws  路  3Comments