2015-12-09 121 views
-1

有人可以幫助我解決以下情況,我正在構建並使用javascript下載csv文件。如何使用javascript構建CSV合併單元格合併

CSV文件應該看起來像這樣。

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. 

問題是,最後一行(*注)因爲它包含逗號(',')而變爲分隔符。 任何幫助或建議將不幫我很多

以下是代碼:

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(); 
+1

你有任何的代碼?和/或樣本輸出? – gotomanners

+2

[處理CSV文件中的逗號]的可能副本(http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – Salketer

+0

[The Great Escapism(Or:What You You需要知道如何處理文本中的文本)](http://kunststube.net/escapism/) – deceze

回答

0

如果你把筆記雙引號內。裏面的逗號將被忽略。含有換行符

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

字段(CRLF),雙引號,和逗號應包含在雙引號。

來源:https://stackoverflow.com/a/769675/3492210