Blob 在生成 csv 文件时跳过第一列

我正在尝试使用 blob 类生成 CSV 文件。第一行工作正常,但其余行正在跳过第一列


var ExcelIndex = ['"user_name","user_hash","authenticate_id","first_name","last_name","description","department","phone_home","phone_mobile","phone_work","phone_other","phone_fax","address_street","address_city","address_state","address_country","address_postalcode","Errors"'];

var dataInSingleStringArray = new Array();

dataInSingleStringArray.push(ExcelIndex[0], "\n");

var ExcelInde = ['"user_name","user_hash","authenticate_id","first_name","last_name","description","department","phone_home","phone_mobile","phone_work","phone_other","phone_fax","address_street","address_city","address_state","address_country","address_postalcode","Errors"'];

// var dataInSingleStringArray = new Array();

dataInSingleStringArray.push(ExcelInde[0], "\n");


window.URL = window.webkitURL || window.URL;

var contentType = 'text/csv';

var csvFile = new Blob([dataInSingleStringArray], {type: contentType});

var a = document.createElement('a');

a.setAttribute("style", "margin-left: 700px;font-size: 2em;")

a.download = 'failed_data.csv';

a.href = window.URL.createObjectURL(csvFile);

a.textContent = 'Download File';

document.getElementById('link').appendChild(a);

<div id="link"></div>


呼啦一阵风
浏览 87回答 1
1回答

呼唤远方

不要每次向数组添加数据时都添加“\n”,而是将数据添加到数组,然后用“\n”将其连接起来&nbsp; &nbsp; &nbsp; var ExcelIndex = ['"user_name","user_hash","authenticate_id","first_name","last_name","description","department","phone_home","phone_mobile","phone_work","phone_other","phone_fax","address_street","address_city","address_state","address_country","address_postalcode","Errors"'];&nbsp; &nbsp; &nbsp; var dataInSingleStringArray = new Array();&nbsp; &nbsp; &nbsp; dataInSingleStringArray.push(ExcelIndex[0]);&nbsp; &nbsp; &nbsp; var ExcelInde = ['"user_name","user_hash","authenticate_id","first_name","last_name","description","department","phone_home","phone_mobile","phone_work","phone_other","phone_fax","address_street","address_city","address_state","address_country","address_postalcode","Errors"'];&nbsp; &nbsp; &nbsp; // var dataInSingleStringArray = new Array();&nbsp; &nbsp; &nbsp; dataInSingleStringArray.push(ExcelInde[0]);&nbsp; &nbsp; &nbsp; //console.log(dataInSingleStringArray);&nbsp; &nbsp; &nbsp; //console.log(dataInSingleStringArray.join("\r\n"));&nbsp; &nbsp; &nbsp; window.URL = window.webkitURL || window.URL;&nbsp; &nbsp; &nbsp; var contentType = 'text/csv';&nbsp; &nbsp; &nbsp; var csvFile = new Blob([dataInSingleStringArray.join("\n")], {type: contentType});&nbsp; &nbsp; &nbsp; var a = document.createElement('a');&nbsp; &nbsp; &nbsp; a.setAttribute("style", "margin-left: 700px;font-size: 2em;")&nbsp; &nbsp; &nbsp; a.download = 'failed_data.csv';&nbsp; &nbsp; &nbsp; a.href = window.URL.createObjectURL(csvFile);&nbsp; &nbsp; &nbsp; a.textContent = 'Download File';&nbsp; &nbsp; &nbsp; document.getElementById('link').appendChild(a);&nbsp; &nbsp;&nbsp;<div id="link"></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript