我正在尝试使用以下功能将数据导出为CSV
function exportHtml(monthNo){
var data = [];
data.push('Person;Salary;Workover');
let query = Meteor.users.find().fetch().map(function(u){
var userFirst = u.profile.name_first;
var userLast = u.profile.name_last;
var currentId = u._id;
try {
var sum = Salaries.findOne({"userId": {$eq: currentId}, "month": {$eq: monthNo}, "year": {$eq: parseFloat(earningFilterYear.get())} } , {sort:{createdAt: -1}}).cost;
var hours = Salaries.findOne({"userId": {$eq: currentId}, "month": {$eq: monthNo}, "year": {$eq: parseFloat(earningFilterYear.get())} } , {sort:{createdAt: -1}}).over;
}catch(e){
}
if(!sum)
sum = 0;
data.push('\r\n');
data.push(userFirst+' '+userLast+';'+sum+';'+hours);
});
var link = document.createElement('a');
link.setAttribute('download', "Export_"+monthNo+"_"+earningFilterYear.get()+".csv");
link.setAttribute('href', 'data:text/html;charset=utf-8;' + data);
link.click();
Bert.alert('Data exported successfully', 'success' );
};
预期行为
Person;Salary;Workover
Name1;5;15
Name2;12;125
现实(一行,忽略未定义)
,Admin Account;295.5;5,,Sergo Sarkjan;0;undefined,,Anton Lidat;0;undefined,,Aleksei Dmijev;0;undefined
似乎新行没有添加,即使我有\ r \ n,我也尝试过没有成功,此外由于某种原因在整个文档中都有这些随机逗号。为什么?
答案 0 :(得分:2)
将data:text/html
更改为data:text/plain
,并使用data
替换为同一行data.join('')
。
它应该是这样的:
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + data.join(''));