CSV is not a good way to store the data the way you want.
You could create objects with MemberName and GroupName properties and then create a CSV file that looks like this:
Or using pretty formatting
To get column names to be the group names you would need to
- create objects that have the all the group names as properties
- each object would contain 3 unrelated people (one per group)
- when you run out of people for a given group you need to stop putting people in that group's property but still need to create a new object because at least one of the remaining groups will have a member
Ugh! I'm having a hard time explaining what it would take to format it the way you want to show it.
In any case, most of your code is excel management code or formatting and a CSV has neither of those. Perhaps it is best to keep your excel code.