Thanks. @j4507. I will try this option when i fail the above ones. See which one can get me there. Appreciate your help.
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 ....
}
}
}
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.
I found this to be a good tutorial on exporting to CSV: http://themeteorchef.com/recipes/exporting-data-from-your-meteor-application/
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);
};
Thanks @riebeekn. Saw this previously, but it was too long to digest. I got a workaround after toying around. Will make this post as solved and post my work around.
Appreciate your help @johnf. I think I will give it a try. But at the moment I have a workaround. Thanks.
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.
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();
@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.
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
Thanks for the update!
I will try have a go using it when I build my export functions.
Thanks for the updates! The meteorpad example is clear and well illustrated the solution. Bookmarked.
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.
In my download button events I have something like:
var link = window.document.createElement("a");
link.setAttribute("href", "data:text/csv;charset=utf-8,%EF%BB%BF" + encodeURI(csv));
link.setAttribute("download", "export_invoices_" + fileName + ".csv");
link.click();
the variable csv
is comma separate string.
I figured out how to achieve this with Papa Parse. Worked so great that I decided to write a tutorial about it. Comes with a repo and an online demo if you want see how it will look. Hope it can help anyone.
Parfait.
Nice work.
Nicely explanation
I found the easiest fix via BOM; just add the magic at beginning of the string, when creating the buffer for the file export:
let buffer = Buffer('\ufeff'+csvString)
Thanks, KENKEN … Great.
I can see correct CSV content with format font on Excell
Did you find a solution? I’m also having trouble with the excel format.