0

I am trying to generate a CSV file using blob class. The first row is working fine but the rest of the rows are skipping the first column

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 Answer 1

1

Instead of adding "\n" each time you add data to your array, Add data to your array then join it with "\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>

Sign up to request clarification or add additional context in comments.

1 Comment

Why we need to add \n while making object of blob class not in every array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.