0

Can some one help me with the following scenario, I am constructing and downloading a csv file using javascript.

The CSV file should look like this.

1. Column1   Column2  Column3
2. aaaaaa    xxxxxxx  rrrrrr
3. bbbbbb    ccccccc  dddddd

*Note: This long statement i, want it to be single statement by merging, of columns.Instead of these statements splitted into columns.

The problem is, the last line(*Note) is getting splitter into columns since it holds commas(','). Any Help or suggestion will do help me a lot

Following is the Code:

var csvRow  = [];
                csvRow.push('Column1','Column2','Column3');
                csvRow.push('aaaaaaa','bbbbbbb','cccccccc');
                csvRow.push('xxxxxxxx','yyyyyyy','zzzzzzzzz');
        csvRow.push('*Note: This long statement i, want it to be single statement by merging, of columns.Instead of these statements splitted into columns.');

        var csvString       =  csvRow.join("\r\n");
        var a               =  document.createElement('a');
        a.href              =  'data:application/csv;charset=utf-8,' + encodeURIComponent(csvString);
        a.target            =  '_blank';
        a.download          =  'SampleCSV.csv';
        document.body.appendChild(a);
        a.click();  
Anand S
  • 1,068
  • 9
  • 22
Sasi Dunston
  • 302
  • 1
  • 4
  • 15

1 Answers1

0

If you put the note inside double quotes. The commas inside it will be ignored.

csvRow.push('"*Note: This long statement i, want it to be single statement by merging, of columns.Instead of these statements splitted into columns."');

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

Source: https://stackoverflow.com/a/769675/3492210

Community
  • 1
  • 1
Anand S
  • 1,068
  • 9
  • 22
  • but that option is shifting content . I want to merge cells for some part and not want to shift it . – Anuj Apr 11 '18 at 05:56