[SOLVED] Export as Excel/CSV


#1

Hi guys, I am looking a way to export some data into downloadable file in excel/cvs format. Anyone has any experiences on this? Or show me a way to do so? Thanks.


#2

netanelgilad:excel package for this, but i don’t understand how to try.


#3

Me too. It has no readme or example on the package. The original npm docs looks a bit daunting. :stuck_out_tongue:


#4

I have used papa parse very successfully to import CSV, and I know they also support export.
This is the package I have used.


#5

If you want to be able to download a table into this format just put this .js file into your client/js folder and you’ll be able to use .tableExport jquery plugin API


#6

Thanks @cstrat, I have seen it is useful to parse JSON to CSV. But i got Papa is undefined error. The document mentioned about using Papa.unparse(). I wonder if i miss anything.

Meanwhile, converting the JSON to CSV is the first step. I am also having issue on making the file downloadable. I am now trying the server side rendering on iron router. No luck so far.


#7

Thanks. @j4507. I will try this option when i fail the above ones. See which one can get me there. Appreciate your help. :smiley:


#8

Ah sorry I haven’t tried unparse before, but my parse code works fine.
I just used Papa.parse(), so nothing out of the ordinary in play.

var results = Papa.parse(fileInput.files[0], {
	skipEmptyLines: true,
	complete: function(results) {
		console.log(results);
		var newInviteObj = {};
		for (var x=1; x<results.data.length; x++) {
		    ... etc ....
		}
	}
}

#9

I found out the package was not added on server side. Other already raise this issue to the author, hope he can make the changes.

Thanks. :smiley:


#10

I found this to be a good tutorial on exporting to CSV: http://themeteorchef.com/recipes/exporting-data-from-your-meteor-application/


#11

Here’s a sample I use in an app, plagiarized from a meteorkitchen.com dataview sample. With this you can click a link on the form and initiate a download of data in a collection. I pulled some sorting & filtering code out cause it was too much to try to post in here, but this should work.

In your view controller:

data: function() {
  return {
  params: this.params || {},
  sample_dataview: Sample.find({},{})
};

Link on your page:

<a href="#" id="dataview-export-csv">Export to CSV</a>

JS Code for your page:

In your template event:

"click #dataview-export-csv": function(e, t) {
   e.preventDefault();
   SamplesViewExport(this.sample_dataview, "csv");
 }

Function your template event calls:

var SamplesViewExport = function(cursor, fileType) {
  var data = cursor.fetch();
  var exportFields = ["samplefield1", "samplefield2", "samplefield3"];
  var str = convertArrayOfObjects(data, exportFields, fileType);
  var filename = "export." + fileType;
  downloadLocalResource(str, filename, "application/octet-stream");
}

Initiate the download:

this.downloadLocalResource = function(data, filename, mimeType) {
  filename = filename || "download";
  mimeType = mimeType || "application/octet-stream";
  var bb = new Blob([data], { type: mimeType });
  var link = document.createElement("a");
  link.download = filename;
  link.href= window.URL.createObjectURL(bb);
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

#12

Thanks @riebeekn. Saw this previously, but it was too long to digest. :stuck_out_tongue: I got a workaround after toying around. Will make this post as solved and post my work around.


#13

Appreciate your help @johnf. I think I will give it a try. But at the moment I have a workaround. Thanks. :smiley:


#14

I have a quick work around as below after looking at @cstrat & @j4507 suggestion.

In the click event handler:

var csv = Papa.unparse(items); // items is in JSON format, use Papa to convert JSON to CSV
window.open(encodeURI('data:text/csv;charset=utf-8,' + csv));

One catch is you cant rename the file. It will be ‘download.csv’. Tried setting the filename= in header, no luck.


#15

This is what I had to do to fix that. So there is probably a similar fix in papa.

Starting at line 278 in the tableExport.js file

var base64data = "base64," + $.base64.encode(excelFile);
				//window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);
				//These are the lines I've added to fix the exporting without an extension
				document.getElementById("dlink").href = 'data:application/vnd.ms-' + defaults.type + ';filename=exportData.doc;' + base64data;
				document.getElementById("dlink").download = defaults.tableName.toString() + '.xls';
				document.getElementById("dlink").click();

#16

@j4507, I have tired this too. I don’t get it ‘download’ a file. I click and nothing happen. Also, my current app is running Chinese character. The base64encode having problem for that. But Papa is ok parsing. :smile:


#17

Hey guys,
Happy to see you found my package and sorry it wasn’t more helpful before.
I created an example and updated the README for the package.
Check out the example meteorpad here: http://meteorpad.com/pad/2hjNqmwHjDvkxvLC5/Leaderboard
And the new README: https://github.com/netanelgilad/meteor-excel
If you found it useful and need some more features, don’t hesitate and open an issue :slight_smile:


#18

Thanks for the update!

I will try have a go using it when I build my export functions.


#19

Thanks for the updates! The meteorpad example is clear and well illustrated the solution. Bookmarked.


#20

How are you guys managing to achieve this? I see lots of ways but none pretty straightforward. @netanelgilad’s approach was the best for me, but it seems I’m unable to download.