1

I am trying to add headers into the CSV file and as a headers I would like to have the variables names used in the WriteLine.

Here you have my code:

using (StreamWriter file = new StreamWriter(fs))                    
{
    for (int s = 0; s < pr.Length; ++s)
    {
        string[] UsersIDS = new string[] {""};
        UsersIDS = db.GetUsersList(pr[s].ProjectID);
        file.WriteLine(
             pr[s].ProjectID + '"' + ',' + 
             '"' +  pr[s].ProjectTitle + '"' + ',' + 
             pr[s].PublishStatus + '"' + ',' + 
             UsersIDS.Length); 
    }
}
6
  • header for the CSV is on the first line. so just do a writeline just after creating the File. Header1,Header2,.. and then start writing data for every line representing each row Commented Sep 2, 2015 at 15:31
  • 1
    So this code is printing out each line in the CSV? You want the header first? Just call file.Writeline before the for loop with whatever you want in header? Commented Sep 2, 2015 at 15:31
  • What is purpose of variable pr? Commented Sep 2, 2015 at 15:35
  • @YuraZaletskyy the variable pr its an array from the web service call that has been initialized before. the csv file is correct but it does not include headers, so I am trying to include them, thanks Commented Sep 2, 2015 at 15:38
  • Hi @AndyWiesendanger yes the file gets the output requested but I am stuck including for each column a header Commented Sep 2, 2015 at 15:41

2 Answers 2

3

I propose you following modification

using (StreamWriter file = new StreamWriter(fs))                    
{
    file.WriteLine("ProjectID, ProjectTitle,PublishStatus,Length");
    for (int s = 0; s < pr.Length; ++s)
    {
        string[] UsersIDS = new string[] {""};
        UsersIDS = db.GetUsersList(pr[s].ProjectID);
        file.WriteLine( pr[s].ProjectID + '"' + ',' + '"' + pr[s].ProjectTitle + '"' + ',' + pr[s].PublishStatus + '"' + ',' + UsersIDS.Length); 

}//end of for
Sign up to request clarification or add additional context in comments.

Comments

2

Just regular WriteLine with constant string seem to be what you are after (unless you want to get names of columns via reflection or some other way):

using (StreamWriter file = new StreamWriter(fs))                    
{
    file.WriteLine( "ProjectID,ProjectTitle,PublishStatus,UsersIDS_Length"); 

    for (int index = 0; index < pr.Length; index++)
    {
        var usersIds = db.GetUsersList(pr[s].ProjectID);
        file.WriteLine("{0},\"{1}\",{2},{3}",
           pr[s].ProjectID, pr[s].ProjectTitle,
           pr[s].PublishStatus, usersIds.Length); 
    }
}

(Sound like you also wanted quotes around title... the rest of quotes looked unbalanced).

1 Comment

Hi, I just tested the suggested modification and its very neat! Sorry I haven't tested correctly before. I really like this one! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.