我正在尝试从我的数据库中以Nodejs和Express为基础将不同类型的数据导出到CSV文件。到目前为止,我已经尝试了几个库,但由于各种原因,似乎没有一个能像我期望的那样工作。
我应该怎么解决这个问题?为了能够将我想要的全部数据导出到CSV文件,我应该掌握什么知识?我该如何强制浏览器执行此操作?
感谢您的帮助。
我应该怎么解决这个问题?为了能够将我想要的全部数据导出到CSV文件,我应该掌握什么知识?我该如何强制浏览器执行此操作?
感谢您的帮助。
// The function gets a list of objects ('dataList' arg), each one would be a single row in the future-to-be CSV file
// The headers to the columns would be sent in an array ('headers' args). It is taken as the second arg
function dataToCSV(dataList,headers){
var allObjects = [];
// Pushing the headers, as the first arr in the 2-dimensional array 'allObjects' would be the first row
allObjects.push(headers);
//Now iterating through the list and build up an array that contains the data of every object in the list, in the same order of the headers
dataList.forEach(function(object){
var arr = [];
arr.push(object.id);
arr.push(object.term);
arr.push(object.Date);
// Adding the array as additional element to the 2-dimensional array. It will evantually be converted to a single row
allObjects.push(arr)
});
// Initializing the output in a new variable 'csvContent'
var csvContent = "";
// The code below takes two-dimensional array and converts it to be strctured as CSV
// *** It can be taken apart from the function, if all you need is to convert an array to CSV
allObjects.forEach(function(infoArray, index){
var dataString = infoArray.join(",");
csvContent += index < allObjects.length ? dataString+ "\n" : dataString;
});
// Returning the CSV output
return csvContent;
}
现在,第二步 - 导出数据: 为了导出数据,在考虑了几个选项后,我发现最方便的方法(对我来说)是通过HTTP头发送数据,并使浏览器将其作为CSV文件下载和解析。我使用以下代码实现了这一点:
//this statement tells the browser what type of data is supposed to download and force it to download
res.writeHead(200, {
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename=*custom_name*.csv'
});
// whereas this part is in charge of telling what data should be parsed and be downloaded
res.end(dataToCSV(dataList,["ID","Name","Date"]),"binary");