我有这个代码,它允许我使用二维数组下载CSV文件,但我需要在第一个数组中的最后一个项目之后有一个换行符。 (就在some other info
之后。
<script type="text/javascript">
// Example data given in question text
var data = [
['name1', 'city1', 'some other info'],
['name2', 'city2', 'more info']
];
// Building the CSV from the Data two-dimensional array
// Each column is separated by ";" and new line "\n" for next row
var csvContent = '';
data.forEach(function(infoArray, index) {
dataString = infoArray.join(';');
csvContent += index < data.length ? dataString + '\n' : dataString;
});
// The download function takes a CSV string, the filename and mimeType as parameters
// Scroll/look down at the bottom of this snippet to see how download is called
var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) { //html5 A[download]
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
}
}
download(csvContent, 'dowload.csv', 'text/csv;encoding:utf-8');
</script>
所以要清楚,我现在得到的结果是:
name1;city1;some other infoname2;city2;more info
我需要它:
name1;city1;some other info
name2;city2;more info
提前感谢。
答案 0 :(得分:1)
可能无论您使用什么来显示文件都需要Windows行结尾而不是UNIX行结尾。请尝试\r\n
,而不仅仅是\n
。