none
How To Save ArrayList of Object Class to CSV file RRS feed

  • Question

  • hi every body

    How To Save ArrayList of Object Class to CSV file .

    I have Object Class like this
    --------------------------------------------------------
    public class Student
    {
    private string _
    StudentName;
    private string _StudentID;

    public string
    StudentName
    {
    get{
    return __StudentName;}

    set{
    _StudentName= value;}
    }

    public string StudentID;
    {
    get{
    return _StudentID;

    set{
    _StudentID= value;}
    }
    }
    ------------------------------------------
    ArrayList stdntArrayList = new ArrayList();

    Student stdnt = new Student();

    stdnt.
    StudentName = "AAAAAA";
    stdnt.
    StudentID = "111111";
    stdntArrayList.Add(stdnt);

    stdnt.StudentName = "BBBBBB";
    stdnt.
    StudentID = "222222";
    stdntArrayList.Add(stdnt);

    -------------------------------------------

    Now how can i save the
    (stdntArrayList) to CSV file with header like this

    =======================================
    CSV file
    =======================================
    StudentName;StudentID
    AAAAAA;111111
    BBBBBB;222222
    CCCCCC;333333
    DDDDDD;444444

    .
    .
    .
    ========================================



    Thursday, November 2, 2006 11:21 PM

Answers

  • going from your description, you need to go through each object in the array list, and get the object and get the properties of that object and write to disk. Example:



    using (StreamWriter theWriter = new StreamWriter(Application.StartupPath + \\file.csv))
    {
       theWriter.WriteLine("StudentName;StudentID");
       foreach(Student curStudent in stdntArrayList)
       {
          theWriter.Write(curStudent.StudentName + ";");
          theWriter.WriteLine(curStudent.StudentID);
       }
    }

     

     

    be sure to import the System.IO namespace to access the StreamWriter

    does this help?

     

    An alternative but pretty much does the same thing:



    System.Text.StringBuilder theBuilder = new System.Text.StringBuilder();
    foreach(Student curStudent in stdntArrayList)
    {
       theBuilder.Append(curStudent.StudentName);
       theBuilder.Append(";");
       theBuilder.Append(curStudent.StudentID);
       theBuilder.Append("\n");
    }
     
    using (StreamWriter theWriter = new StreamWriter(Application.StartupPath + \\file.csv))
    {
       theWriter.Write(theBuilder.ToString());  
    }

     

    Friday, November 3, 2006 1:34 AM
    Moderator

All replies

  • going from your description, you need to go through each object in the array list, and get the object and get the properties of that object and write to disk. Example:



    using (StreamWriter theWriter = new StreamWriter(Application.StartupPath + \\file.csv))
    {
       theWriter.WriteLine("StudentName;StudentID");
       foreach(Student curStudent in stdntArrayList)
       {
          theWriter.Write(curStudent.StudentName + ";");
          theWriter.WriteLine(curStudent.StudentID);
       }
    }

     

     

    be sure to import the System.IO namespace to access the StreamWriter

    does this help?

     

    An alternative but pretty much does the same thing:



    System.Text.StringBuilder theBuilder = new System.Text.StringBuilder();
    foreach(Student curStudent in stdntArrayList)
    {
       theBuilder.Append(curStudent.StudentName);
       theBuilder.Append(";");
       theBuilder.Append(curStudent.StudentID);
       theBuilder.Append("\n");
    }
     
    using (StreamWriter theWriter = new StreamWriter(Application.StartupPath + \\file.csv))
    {
       theWriter.Write(theBuilder.ToString());  
    }

     

    Friday, November 3, 2006 1:34 AM
    Moderator
  • Thanks ahmedilyas

    Both work perfectly


    God Bless You
    Friday, November 3, 2006 10:51 AM
  • hi, how would you read this data back, like when I open my application i want the arraylist to all come back so i can manipulate it as normal with my methods and forms, etc., but upon closing the app i want to save the entire list again
    Tuesday, September 28, 2010 3:42 AM
  • I strangely get's error's when i type like:

     System.Text.StringBuilder theBuilder = new System.Text.StringBuilder();
                foreach(Video curVideo in video)
                {
                   theBuilder.Append(curVideo.artNr);
                   theBuilder.Append(";");
                   theBuilder.Append(curVideo.title);
                   theBuilder.Append(";");
                   theBuilder.Append(curVideo.year);
                   theBuilder.Append("\n");
                }
     
                    using (StreamWriter theWriter = new StreamWriter(Application.StartupPath + file.csv))
                    {
                       theWriter.Write(theBuilder.ToString()); 
                    }

    The error's says:

    "Error    1    The name 'Application' does not exist in the current context"

    "Error    2    The name 'file' does not exist in the current context4"

    What an I doing wrong?

    Tuesday, May 17, 2011 8:59 AM
  • Hi Cricka,

    With regards to Error    1 :  Please include the following namespace  : System.Windows.Forms

    With regards to Error    2:  Ensure that in bin\Debug there is a CSV file named file.csv and then Ensure that the file.csv is given within double quotes and check if it is not blank.

    Comments are welcome.

    Regards.

    Sushil Kumar P

    Friday, July 15, 2011 7:32 AM