为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼啦一阵风 2023-10-20 16:14:56
我正在尝试使用 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>
查看完整描述

1 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

不要每次向数组添加数据时都添加“\n”,而是将数据添加到数组,然后用“\n”将其连接起来


      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]);


      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]);

      //console.log(dataInSingleStringArray);

      //console.log(dataInSingleStringArray.join("\r\n"));



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

      var contentType = 'text/csv';

      var csvFile = new Blob([dataInSingleStringArray.join("\n")], {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>


查看完整回答
反对 回复 2023-10-20
  • 1 回答
  • 0 关注
  • 83 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信