2017-05-24 92 views
-1

我使用此代碼省略了引號並用逗號分隔。 我有這樣的數據csv文件的內容。 ex:
「1111」,「05-24-2017」,「08:30」,「0」,「TRAVEL」
「2222」,「05-25-2017」,「08:20」, 「0」,「旅行」用雙引號括起來,並用csv文件中的逗號分隔

我用這個代碼:

public bool ReadEntrie(int id, ref string name) 
    { 
     int count = 0; 
     CreateConfigFile(); 
     try 
     { 
      fs = new FileStream(data_path, FileMode.Open); 
      sr = new StreamReader(fs); 
      bool cond = true; 
      string temp = ""; 
      while (cond == true) 
      { 
       if ((temp = sr.ReadLine()) == null) 
       { 
        sr.Close(); 
        fs.Close(); 
        cond = false; 
        if (count == 0) 
         return false; 
       } 
       if (count == id) 
       { 
        string[] stringSplit = temp.Trim('\"').Split(new 
        String[] { "," }, StringSplitOptions.None); 
        //string[] stringSplit = temp.Split(','); 
        int _maxIndex = stringSplit.Length; 
        name = stringSplit[0];      
       } 
       count++; 
      } 
      sr.Close(); 
      fs.Close(); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    }      
+0

什麼是預期的輸出 –

+0

向後:temp.Split(新的String [] { 「」}, StringSplitOptions.None)。選擇(X => x.Trim( '\「')) .ToArray(); – jdweng

+0

我想顯示數據到我的窗體,組成文本框,datetimepicker,combobox.I在閱讀datetimepicker的字段時遇到錯誤。它是由於csv文件的格式是用雙引號括起來,而在我有一個自定義格式的datetimepicker我設置了 – Sam

回答

0
The problem is not on that function. It is by other function and I used trim 
for it.                
string[] stringSplit = temp.Split(','); 
int _maxIndex = stringSplit.Length; 
name = stringSplit[0].Trim('"'); 
lastname = stringSplit[1].Trim('"'); 
phone = stringSplit[2].Trim('"'); 
mail = stringSplit[3].Trim('"'); 
website = stringSplit[4].Trim('"'); 
1

如果沒有逗號引號的數據,例如一部分

"12,34","56","a""bc" -> 12,34 56 a"bc 

,你可以把一個簡單的的Linq

string[][] result = File 
    .ReadLines(@"C"\MyData.csv") 
    .Select(line => line 
     .Split(',') 
     .Select(item => item.Trim('"')) 
     .ToArray()) 
    .ToArray(); 

進一步改善是返回一個定製類的數組:

MyClass[] result = File 
    .ReadLines(@"C"\MyData.csv") 
    .Select(line => line 
     .Split(',')) 
    .Select(items => new MyClass() { 
     Id = items[0].Trim('"'), 
     Date = DateTime.ParseExact(items[1].Trim('"') + " " + items[2].Trim('"'), 
           "MM-dd-yyyy hh:mm", 
           CultureInfo.InvariantCulture), 
     Code = items[3].Trim('"'), 
     Text = items[4].Trim('"'), 
    }) 
    .ToArray(); 
+0

當我使用ReadLines命令時,我遇到了錯誤的文件被其他應用程序使用 – Sam

+0

@Sam:檢查你的代碼:看起來你不關閉文件另一種可能是該文件是可打開的由MS Excel或其他東西打開它寫作(因此想要exlusive訪問) –

+0

好吧。我有你的想法有關修剪。我現在工作。感謝您的幫助。 – Sam

0

試試這個簡單的功能。它將處理雙引號和雙引號之間的逗號。

private string[] GetCommaSeperatedWords(string sep, string line) 
{ 
    List<string> list = new List<string>(); 
    StringBuilder word = new StringBuilder(); 
    int doubleQuoteCount = 0; 
    for (int i = 0; i < line.Length; i++) 
    { 
     string chr = line[i].ToString(); 
     if (chr == "\"") 
     { 
      if (doubleQuoteCount == 0) 
       doubleQuoteCount++; 
      else 
       doubleQuoteCount--; 

      continue; 
     } 
     if (chr == sep && doubleQuoteCount == 0) 
     { 
      list.Add(word.ToString()); 
      word = new StringBuilder(); 
      continue; 
     } 
     word.Append(chr); 
    } 

    list.Add(word.ToString()); 

    return list.ToArray(); 
} 
相關問題