-1

I have two classes which look like this:

 public class SortedUser
    {
        public string Email { get; set; }

        public List<IpInfo> IPAndCountries = new List<IpInfo>();
    }

And then:

 public class IpInfo
    {

        public string Ip { get; set; }


        public string Hostname { get; set; }


        public string City { get; set; }


        public string Region { get; set; }


        public string Country { get; set; }


        public string Loc { get; set; }


        public string Org { get; set; }


        public string Postal { get; set; }

    }

I would like to create a reasonably readable file (txt, csv or even xls/xlsx) from this which could look something:

User1 Email

IP1 Country City
IP2 Country City
IP3 Country City
and so on...

User2 Email

IP1 Country City
IP2 Country City
IP3 Country City
and so on...

User3 Email

IP1 Country City
IP2 Country City
IP3 Country City
and so on...

What is the easiest way to perform this? Can someone help me out?

P.S. It can even be a PDF file (which would be the best).

User987
  • 3,663
  • 15
  • 54
  • 115
  • 1
    Possible duplicate of [Writing data into CSV file in C#](https://stackoverflow.com/questions/18757097/writing-data-into-csv-file-in-c-sharp) – mjwills Aug 04 '18 at 12:52

1 Answers1

1

Try following :

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            SortedUser sortedUser = new SortedUser();
            sortedUser.Output(FILENAME);
        }
    }
    public class SortedUser
    {
        public static List<SortedUser> sortedUsers = new List<SortedUser>();

        public string Email { get; set; }
        public List<IpInfo> IPAndCountries = new List<IpInfo>();

        public void Output(string filename)
        {
            StreamWriter writer = new StreamWriter(filename);
            string line = "";


            line = string.Join(",",new string[] {"Email","Ip","Hostname", "City", "Region", "Country", "Loc", "Org", "Postal"});
            writer.WriteLine(line);

            foreach (SortedUser user in sortedUsers)
            {
                foreach (IpInfo ipInfo in user.IPAndCountries)
                {
                    line = string.Join(",",
                        user.Email,
                        ipInfo.Ip,
                        ipInfo.Hostname,
                        ipInfo.City,
                        ipInfo.Region,
                        ipInfo.Country,
                        ipInfo.Loc,
                        ipInfo.Org,
                        ipInfo.Postal
                    );
                    writer.WriteLine(line);
                }
            }

            writer.Flush();
            writer.Close();
        }
    }


 public class IpInfo
    {
        public string Ip { get; set; }
        public string Hostname { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string Country { get; set; }
        public string Loc { get; set; }
        public string Org { get; set; }
        public string Postal { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20