2017-10-05 58 views
1

我做了一些研究,但沒有運氣,還是相對很新的總體規劃,對代碼效率任何提示都大加讚賞。如何將我的收藏內容寫入CSV文件?

那麼我將如何去運行它通過我的分級功能中的一個或更好,但一個CSV文件,該文件是逗號分隔後寫我的收藏+等級的內容出一個文本文件。

我不斷收到類似的問題cannot convert from 'string' to string[]。我知道我可能在這裏錯過了一步,但我很難弄清楚它是什麼。

這是我的列表格式,我從一個CSV文件中加載的值,然後分別存入FirstLastName和MarksValue。

下面是我的分級單選按鈕,我設法讓他們打印我的函數+ FirstLastName和MarksValue的輸出到消息框,所以這是我的一個開始。

public class Marks 
{  
    public string FirstLastName { get; set; } 
    public int MarksValue { get; set; } 
    //public string mySecondaryGrade { get; set; } 
} 

private void buttongradeStart_Click(object sender, RoutedEventArgs e) 
{ 
    // This runs through an if statement based on my radio buttons. 
    //Testing that radio buttons work with my if else if statements 
    // will change values to my grade function later 
    Grade grader = new Grade(); 
    VET vetGrader = new VET(); 
    PolyOveride overideVet = new PolyOveride(); 
    Helper myhelperArray = new Helper(); 
    string v; 

    //This is a check to see if any data has been loaded. 
    if (dLoad == true) 
    { 

     //var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
     System.Windows.Forms.SaveFileDialog saveDialogue = new System.Windows.Forms.SaveFileDialog(); 
     saveDialogue.Title = "Save File"; 
     saveDialogue.FileName = "Graded-Assessment1.csv"; 
     saveDialogue.Filter = "CSV Files(*.csv)|*.csv|Text Files(*.txt)|*.txt|All files(*.*)|*.*"; 
     saveDialogue.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
     if (saveDialogue.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      var path = saveDialogue.FileName; 
      if (radioCollegeGrade.IsChecked == true) 
      { 
       foreach (Marks mMarks in myhelperArray) 
       { 
        File.WriteAllLines(path, v); 
        v = grader.CollegeGrade(mMarks.MarksValue); 
         System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
        //System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.csv"); 
        //file.WriteLine(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
        //file.Close(); 
       } 
      } 
      else if (radioVetGrade.IsChecked == true) 
      { 
       foreach (Marks mMarks in myhelperArray) 
       { 
        v = vetGrader.VETGrade(mMarks.MarksValue); 
         System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
       } 
      } 
      else if (radioCompetency.IsChecked == true) 
      { 
       foreach (Marks mMarks in myhelperArray) 
       { 
        v = overideVet.VETGrade(mMarks.MarksValue); 
         System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
       } 
      } 
      else 
      { 
       System.Windows.Forms.MessageBox.Show("I am not sure how you deslected a box!, you should probably select an option."); 
      } 
     } 
     else 
     { 
      System.Windows.Forms.MessageBox.Show("Placeholder Message"); 
     } 
    } 
    else 
    { 
     System.Windows.Forms.MessageBox.Show("You have not selected a file for grading"); 
    } 
} 

不知道有關這是怎樣,但是這是我的助手類

public class Helper 
{ 
    public static List<Marks> arrayCSV = new List<Marks>(); 
    public IEnumerator<Marks> GetEnumerator() { return arrayCSV.GetEnumerator(); } 

} 
+3

我建議使用CsvHelper NuGet包 - 見https://joshclose.github.io/CsvHelper/的例子。編寫自己的csv輸出看起來很簡單,但有很多問題。例如,如果用戶名有逗號,會發生什麼情況?站在巨人的肩膀上! – tofutim

+0

好文檔那裏交配,謝謝。 –

+0

_站在巨人的肩膀上!_ ..並希望巨人不會倒下;) – Fabio

回答

0

嗯,我結束了在一個非常基本的層面解決它,它仍然有很大的提升空間,尋找轉發嘗試CsvHelper nuget包。我最初想要的東西非常簡單,不知道爲什麼花了我很長時間才弄清楚。

我的一個小時左右的代碼盯着後,我意識到我需要到第三值添加到我的商標列表,加入我的配光功能輸出到我們的名單。然後我使用StreamWriter將我列表中的每個單獨部分寫入一個csv文件。

我最初的錯誤是試圖寫MarksValue + FirstLastName + V將for-each循環中的一個文本文件,而無需更新列表。

在實現所有這些之後,我取消註釋了mySecondaryGrade,它已經是我們列表中的一個值,然後將字符串v的內容保存到mySecondaryGrade中。仍然有一些異常處理和整理工作要做,但能夠正常工作併產生結果是很好的。

public class Marks 
{  
    public string FirstLastName { get; set; } 
    public int MarksValue { get; set; } 
    public string mySecondaryGrade { get; set; } 
} 

private void buttongradeStart_Click(object sender, RoutedEventArgs e) 
    { 
     // This runs through an if statement based on my radio buttons. 
     //Testing that radio buttons work with my if else if statements 
     // will change values to my grade function later 
     Grade grader = new Grade(); 
     VET vetGrader = new VET(); 
     Marks secMarks = new Marks(); 
     PolyOveride overideVet = new PolyOveride(); 
     Helper myhelperArray = new Helper(); 

     //string x; 
     if (dLoad == true) 
     { 

      //var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
      System.Windows.Forms.SaveFileDialog saveDialogue = new System.Windows.Forms.SaveFileDialog(); 
      saveDialogue.Title = "Save File"; 
      saveDialogue.FileName = "Graded-Assessment1.csv"; 
      saveDialogue.Filter = "CSV Files(*.csv)|*.csv|Text Files(*.txt)|*.txt|All files(*.*)|*.*"; 
      saveDialogue.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
      if (saveDialogue.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       //var mySecondaryGrade = secMarks.mySecondaryGrade; 
       //IMPORTANT I NEED TO ADD THE GRADE OUTPUT TO MY LIST AND NOT JUST DISPLAY OUTPUT IN TEXTBOX 
       string v; 
       if (radioCollegeGrade.IsChecked == true) 
       { 
        foreach (Marks mMarks in myhelperArray) 
        { 
         v = grader.CollegeGrade(mMarks.MarksValue);        
         System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
         mMarks.mySecondaryGrade = v; 
         //dataGridMarks.Columns[3] = mMarks.mySecondaryGrade; 
        } 

        using (var writer = new StreamWriter(saveDialogue.FileName)) 
        { 
         foreach (Marks writeMarks in myhelperArray) 
         { 
          writer.WriteLine(writeMarks.FirstLastName + "," + writeMarks.MarksValue + "," + writeMarks.mySecondaryGrade); 
         } 
        } 

       } 
       else if (radioVetGrade.IsChecked == true) 
       { 
        foreach (Marks mMarks in myhelperArray) 
        { 
         v = vetGrader.VETGrade(mMarks.MarksValue); 
         System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
         mMarks.mySecondaryGrade = v; 
        } 

        using (var writer = new StreamWriter(saveDialogue.FileName)) 
        { 
         foreach (Marks writeMarks in myhelperArray) 
         { 
          writer.WriteLine(writeMarks.FirstLastName + "," + writeMarks.MarksValue + "," + writeMarks.mySecondaryGrade); 
         } 
        } 
       } 
       else if (radioCompetency.IsChecked == true) 
       { 
        foreach (Marks mMarks in myhelperArray) 
        { 
         v = overideVet.VETGrade(mMarks.MarksValue); 
         System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v); 
         mMarks.mySecondaryGrade = v; 
        } 

        using (var writer = new StreamWriter(saveDialogue.FileName)) 
        { 
         foreach (Marks writeMarks in myhelperArray) 
         { 
          writer.WriteLine(writeMarks.FirstLastName + "," + writeMarks.MarksValue + "," + writeMarks.mySecondaryGrade); 
         } 
        } 
       } 
       else 
       { 
        System.Windows.Forms.MessageBox.Show("I am not sure how you deslected a box!, you should probably select an option."); 
       } 
      } 
      else 
      { 
       System.Windows.Forms.MessageBox.Show("Placeholder Message"); 
      } 
     } 
     else 
     { 
      System.Windows.Forms.MessageBox.Show("You have not selected a file for grading"); 
     } 
    } 

Result