title: How to write csv file using javascript?
body: <p>I am creating one array dynamically.Following array is one example.</p>
  
  <pre><code>textarray = [["label first","details of label 1"],["label sec","details of label 2"],["label 3","details of label 3"],["label 4","details of label 4 and 3"],["label 5","details of label 5"],["label 6","details of label 6"],["label 7","details of label 7"],["label 8","details of label 8"],["label 9","details of label 9"]]
  </code></pre>
  
  <p>My ultimate goal is onclick on save button array will be generated and it should ask me for save/open in <strong>EXCEL format</strong>. So i have written something like this,</p>
  
  <pre><code>var a = document.createElement('a');
  a.href     = 'data:attachment/csv;charset=utf-8,' +textarray;
  a.target   = '_blank';
  a.download = 'myFile.csv';
  document.body.appendChild(a);
  a.click();
  </code></pre>
  
  <p>I am getting prompt to save or open csv file but if i open that file, contents are not as expected(not showing properly)</p>
  
  <p>It is coming like this,</p>
  
  <pre><code>            **column1**   **column2**   ...........
  **row1**    labelfirst  detailsoflabel1  labelsec  detailsoflabel2
  </code></pre>
  
  <p>One row should contain only one element and if you see the above output there is no space in between words(see the array). It should be like this,</p>
  
  <pre><code>               **column1**       **column2**
  
   **row1**      label first    details of label 1
  
   **row2**      label sec      details of label 2
  
      .
      .
      .
  </code></pre>
  
  <p>Please help me.</p>