Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9114723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:15:51+00:00 2026-06-17T04:15:51+00:00

Having problems with formatting CSV created from C# code. In the notepad file the

  • 0

Having problems with formatting CSV created from C# code. In the notepad file the output scrolls vertically down one row (the values seen in the structs below are output in one row. There is a row of numbers as well that appears directly below the struct values but the numbers should be in a new row beside the structs). When I open in excel it’s a similar story only the output from the structs is where it should be however the row of numbers appears directly below the struct values but one row to the right if that makes sense, and the numbers should appear directly beside their corresponding struct values. The code I’m using is below.

Here are the structs for the dictionaries im working with.

public enum Genders
{
    Male,
    Female,
    Other,
    UnknownorDeclined,
}

public enum Ages
{
    Upto15Years,
    Between16to17Years,
    Between18to24Years,
    Between25to34Years,
    Between35to44Years,
    Between45to54Years,
    Between55to64Years,
    Between65to74Years,
    Between75to84Years,
    EightyFiveandOver,
    UnavailableorDeclined,
}

the csv file that does the outputting using a streamwriter and stringbuilder.

public void CSVProfileCreate<T>(Dictionary<T, string> columns, Dictionary<T, int> data)
{
        StreamWriter write = new StreamWriter("c:/temp/testoutputprofile.csv");
        StringBuilder output = new StringBuilder();
        foreach (var pair in columns)
        {
            //output.Append(pair.Key);
            //output.Append(",");
            output.Append(pair.Value);
            output.Append(",");
            output.Append(Environment.NewLine);
        }

        foreach (var d in data)
        {
            //output.Append(pair.Key);
            output.Append(",");
            output.Append(d.Value);
            output.Append(Environment.NewLine);
        }

        write.Write(output);
        write.Dispose();
}

And finally the method to feed the dictionaries into the csv creator.

    public void RunReport()
    {
        CSVProfileCreate(genderKeys, genderValues);
        CSVProfileCreate(ageKeys, ageValues);
    }

Any ideas?

UPDATE

I fixed it by doing this:

    public void CSVProfileCreate<T>(Dictionary<T, string> columns, Dictionary<T, int> data)
    {
        StreamWriter write = new StreamWriter("c:/temp/testoutputprofile.csv");
        StringBuilder output = new StringBuilder();

        IEnumerable<string> col = columns.Values.AsEnumerable();
        IEnumerable<int> dat = data.Values.AsEnumerable();

        for (int i = 0; i < col.Count(); i++)
        {
            output.Append(col.ElementAt(i));
            output.Append(",");
            output.Append(dat.ElementAt(i));
            output.Append(",");
            output.Append(Environment.NewLine);
        }

        write.Write(output);
        write.Dispose();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:15:52+00:00Added an answer on June 17, 2026 at 4:15 am

    You write Environment.NewLine after every single value that you output.

    Rather than having two loops, you should have just one loop that outputs

    • A “pair”
    • A value
    • Environment.NewLine

    for each iteration.

    Assuming columns and data have the same keys, that could look something like

    foreach (T key in columns.Keys)
    {
        pair = columns[key];
        d = data[key];
        output.Append(pair.Value);
        output.Append(",");
        output.Append(d.Value);
        output.Append(Environment.NewLine);
    }
    

    Note two complications:

    • If pair.Value or d.Value contains a comma, you need to surround the output of that cell with double quotes.
    • If If pair.Value or d.Value contains a comma and also contains a double-quote, you have to double up the double-quote to escape it.

    Examples:

    Smith, Jr

    would have to be output

    “Smith, Jr”

    and

    “Smitty” Smith, Jr

    would have to be output

    “””Smitty”” Smith, Jr”

    UPDATE

    Based on your comment about the keys…

    For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.

    http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

    If you cannot use the key to associate the right pair with the right data, how do you make that association?

    If you are iterating the dictionary and they happen to be in the order you hope, that is truly undefined behavior that could change with the next .NET service pack.

    You need something reliable to relate the pair with the correct data.

    About the var keyword

    var is not a type, but rather a shortcut that frees you from writing out the entire type. You can use var if you wish, but the actual type is KeyValuePair<T, string> and KeyValuePair<T, int> respectively. You can see that if you write var and hover over that keyword with your mouse in Visual Studio.

    About disposing resources

    Your line

    write.Dispose();
    

    is risky. If any of your code throws an Exception prior to reaching that line, it will never run and write will not be disposed. It is strongly preferable to make use of the using keyword like this:

    using (StreamWriter write = new StreamWriter("c:/temp/testoutputprofile.csv"))
    {
        // Your code here
    }
    

    When the scope of using ends (after the associated }), write.Dispose() will be automatically called whether or not an Exception was thrown. This is the same as, but shorter than,

    try
    {
        StreamWriter write = new StreamWriter("c:/temp/testoutputprofile.csv");
        // Your code here
    }
    finally
    {
        write.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem with formatting the output from foreach loop. What should
I am having a problem with formatting a date that I receive from a
Im having problems with a method in a class file: public static function getPageLeft($pid)
im having problems with formatting for a UITextView. my app pulls in XML, saves
I'm having some problems formatting the decimals of a double. If I have a
I'm having a problem with formatting date with date() function. My code is: <?
I have a csv file where each row defines a room in a given
I'm having problems embedding php inside an html file. I first ran in to
I am having problems with a table. It's made of 3 cells each row,
Having problems with a small awk script, Im trying to choose the newest of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.