Adding headers into CSV output file using StreamWriter

断了今生、忘了曾经 提交于 2019-12-06 16:17:38

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

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!