How do I use packages for importing and exporting excel datasheets?

I am new to meteorjs and I tried using ‘netanelgilad:excel’ , ‘meteor npm install xlsx’ etc but to no use.

I want to access the data inside an uploaded excel file. Here is my main.js client code :-

‘click #uploadData’(event, instance) {
// Prevent default browser form submit
var input = document.getElementById(“fileSelect”).files[0];
console.log(“read file”);

XLSX = require('xlsx');
fs = require('fs');
workbook = XLSX.readFile(input);
var yourSheetsName = workbook.SheetNames;
var a = XLSX.utils.sheet_to_csv(workbook[0]);
console.log(a);

}

Here is my HTML code:-

--webNetUse--

Upload Data

I dont know whats going wrong and where! If it is got to do something with loading packages please show me how to do it correctly and efficiently.

Thanks in advance!

https://atmospherejs.com/huaming/js-xlsx is really good, i copy pasted some code and viola or do preprocessor to convert xls to csv and with papaparse upload it with method

Hey Muphet, Thanks for your response!
My issue is that XLSX.readFile is not being recognised by meteor. There seems to be some issue in loading packages. Tried npm install xlsx - even that does not work.

I tried XLSX=npm.require(‘xlsx’) and many more such combos but in vain.

What I need is simple extraction of data from the excel file.

Before you get into the code, make sure you are using your npm stuff correctly:

// cli
meteor npm install xlsx


// client.js
import XLSX from 'xlsx';

Template.myTemplate.onCreated(function () {
  console.log(XLSX, XLSX.readFile);
});

1 Like

Thanks for the reply! But the use of readFile() gives me the following error :

readFileSync is not a function.

PS : sorry for late response.

Yeah, if you are using things like readFileSync, they will have to be on the server. I would put your code in a Method and return the data.

I want to access the data inside and upload excel file. I am writing below code in server side but getting error like Exception while invoking method ‘filehere’ undefined

Meteor.methods({
filehere(){

	var fs = Npm.require('fs');
	var path = Npm.require('path');
	var basepath = path.resolve('.').split('.meteor')[0];
           console.log(basepath);
	var excel = new Excel('xls');
	var workbook = excel.readFile(basepath+'server/MeteorJs.xls');
	//var youSheetName = workbook.sheetNames;
	/*workbook = XLSX.readFile(inputname);
	var sheetNames = workbook.sheetNames;
*/
}

});

can you solve this issue???

Thanks

wrote the code on server side main.js but unable to access the workbook yet.
The console displays “Undefined” when console.log(yourSheetsName); is run.
I used exceljs package for this.

extract(filename){
console.log(“In Server!!”);
console.log(filename);
var fs = Npm.require(‘fs’);
var path = Npm.require(‘path’);
var basepath = path.resolve(’.’).split(’.meteor’)[0];
basepath = basepath + ‘server/5+2–Network-05-05-16.xls’;
console.log(basepath);

    var Excel = require('exceljs');
	var workbook = new Excel.Workbook();
workbook.xlsx.readFile(basepath);
	var yourSheetsName = workbook.SheetNames;
 
    console.log(yourSheetsName);
}

Any help would be much appreciated.

Thanks