2017-09-26 72 views
-4

我在我的程序中有一些字符串變量,我需要把它放到CSV文件中。例如c#格式化CSV

var1 = "abc";  
var2 = "bbb";  
var3 = "  
-vvv  
-xxx  
-zzz  
-ccc  
-ddd  
"; // Var 3 is a multiline string, need it to be put in one cell 

CSV文件:

Col1 Col2 Col3 (1 cell) 
abc bbb -vvv 
       -xxx 
       -zzz 
       -ccc 
       -ddd 

然後在下一行: VAR1(的newval)VAR2(的newval)VAR3(的newval)

的問題是如何甲酸鹽CSV文件變種X雲以colx等等。

我的代碼如下所示:

string getDir = Directory.GetCurrentDirectory(); 

DirectoryInfo d = new DirectoryInfo(@getDir + "\\src"); 
FileInfo[] Files = d.GetFiles("*.txt"); 
string str = ""; 
foreach(FileInfo file in Files) 
{ 
    str = str + "," + file.Name;    
} 

string lines="col1," + "col2," + "col3," + "col4 ," + "col5," + "col6," + "\n"; 

int ncheck = 1; 
int countFiles = d.GetFiles().Length; 

int vcheck = 0; 



    while (ncheck <= /*countFiles*/1) 
    { 
     var getfile = str.Split(',')[ncheck]; 
     lines = lines + getfile;  
     while (vcheck <= 4) 
     { 
     string startSTR = "a1,a2,a3,a4,a5"; 
     var starts = startSTR.Split(',')[vcheck]; 

     string endSTR = "b1,b2,b3,b4,b5"; 
     var ends = endSTR.Split(',')[vcheck]; 


     string St = System.IO.File.ReadAllText(d + "\\" + getfile); 

     int pFrom = St.IndexOf(starts) + starts.Length; 
     if (St.IndexOf(starts) == -1 || St.IndexOf(ends) == -1) {pFrom=0;}; 
     int pTo = St.LastIndexOf(ends); 
     if (St.IndexOf(starts) == -1 || St.IndexOf(ends) == -1) {pTo=pFrom+0;}; 




     String result = St.Substring(pFrom, pTo - pFrom); 
     if (St.IndexOf(starts) == -1 || St.IndexOf(ends) == -1) {result="Not found";}; 

     /*Console.WriteLine(starts); 
     Console.WriteLine(result);   
     Console.WriteLine(ends);*/ 
     Console.WriteLine(lines); 
     lines = lines + "," + result; 
     Console.WriteLine(lines); 
     vcheck++; 
     } 
     lines = lines + "\n"; 
     ncheck++; 
     vcheck=0; 
    } 

System.IO.StreamWriter filetxt = new System.IO.StreamWriter(@getDir + "\\test.csv"); 
filetxt.WriteLine(lines); 

filetxt.Close(); 

它正在爲txt文件,然後進行字符串搜索src目錄,最後保存到CSV文件。正如我之前所說的,由於一個字符串有多行需要在一個單元格中,所以我在格式化文檔時遇到了問題。

有沒有更好的方法來解決這個問題?

+1

那麼問題是什麼? – CalC

+1

請發佈您迄今爲止編寫CSV文件的代碼 –

+0

到目前爲止我只使用分隔符,但有沒有更好的方法通過c#在選定的單元格中放置一些var? – michal

回答

0

如果你在你的字符串中的逗號或換行,你必須報串

原文:約翰史密斯,JR 引用:「約翰·史密斯,JR」

如果你有一個報價在字符串中,用雙引號逃避它,它

原文:約翰「約翰尼」史密斯 引用:約翰「」約翰尼「」史密斯

結合這兩個規則,如果你有逗號/換行符和字符串中的引號。

原文:約翰「約翰尼」史密斯,JR 引用:「約翰‘’約翰尼」「史密斯,JR」

這裏的擴展方法,我寫的是正確處理引用。像這樣使用它:

var properQuoted = myString.CsvQuote();

static public string CsvQuote(this string text) 
    { 
     if (text == null) 
     { 
      return string.Empty; 
     } 

     bool containsQuote = false; 
     bool containsComma = false; 
     bool containsNewline = false; 
     bool containsCR = false; 
     int len = text.Length; 
     for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) 
     { 
      char ch = text[i]; 
      if (ch == '"') 
      { 
       containsQuote = true; 
      } 
      else if (ch == ',') 
      { 
       containsComma = true; 
      } 
      else if (ch == '\r') 
      { 
       containsCR = true; 
      } 
      else if (ch == '\n') 
      { 
       containsNewline = true; 
      } 
     } 

     bool mustQuote = containsComma || containsQuote || containsCR || containsNewline; 

     if (containsQuote) 
     { 
      text = text.Replace("\"", "\"\""); 
     } 

     if (mustQuote) 
     { 
      return "\"" + text + "\""; // Quote the cell and replace embedded quotes with double-quote 
     } 
     else 
     { 
      return text; 
     } 
    } 
+0

謝謝,但我也需要知道如何將某個變量的字符串放到指定單元格中。例如var1到B5單元格等。 – michal

+0

您可以根據文本文件的行號定位行。在Excel中打開時,輸出文件的第五行是「5」。這些列是通過在文件中輸出一個逗號來完成的。正確逃脫的逗號不計算在內。 「」一個,呃?「,」二,三,四,五「將在colun B中看到文本」Two「(請注意第一個單元格數據中的逗號用引號括起)。 –

+0

是的,謝謝你,我已經修好了,謝謝你的回答,這很有幫助。 – michal