2017-07-13 43 views
0

我試圖將變量寫入CSV文件中的一行,其中每個變量位於不同的單元格中。我的問題是,所有的變量都寫入到同一個小區試圖用「追加」和「AppendLine」不能把多個參數將多個變量寫入一個CSV行

時,這是我的代碼:

var NBSref = textBox3.Text; 
    var Lenght = textBox1.Text; 
    var Width = textBox2.Text; 


    string csvpath = "FILE LOCATION"; 

    StringBuilder csvcontentNew = new StringBuilder(); 
    csvcontentNew.Append(NBSref + Lenght + Width); // This is my issue 




    //String that will define CSV Location 

    File.AppendAllText(csvpath, csvcontentNew.ToString()); 


    textBox1.Clear(); 
    textBox2.Clear(); 
    textBox3.Clear(); 

當我使用「追加」所有的變量數據插入到我的文件,但低於

This is what I get from my program

都在同一個小區看到,但是這是我想要的:

Ideally what my program would output

回答

2

CSV的名字給你的答案是:「逗號分隔值」。您需要用逗號分隔變量,以便在任何閱讀器(如MSExcel)中打開時將它們分開。

var NBSref = textBox3.Text; 
var Lenght = textBox1.Text; 
var Width = textBox2.Text; 

var separator = ","; 

string csvpath = "FILE LOCATION"; 

StringBuilder csvcontentNew = new StringBuilder(); 
csvcontentNew.Append(NBSref + separator + Lenght + separator + Width); // Try it like this 

//String that will define CSV Location 

File.AppendAllText(csvpath, csvcontentNew.ToString()); 

textBox1.Clear(); 
textBox2.Clear(); 
textBox3.Clear(); 
0

MS Excel使用「區域和語言選項」(Windows控制面板)中的列表分隔符集。對於大多數地區來說,它是逗號,但對於意大利語這樣的語言來說,它是分號。 請記住,如果在單元格中使用分隔符char作爲值,則必須雙引號引用整個單元格值(雙引號,因爲分隔符可能會在不同區域中更改)。