0

I have a List<Person> which is bound to a grid view. I want to export all the values to an excel file. My person class is as follows:

class Person
{
    public string Name { get; set; }
    public string City { get; set; }
    public int Age { get; set; }
}

Is there any way to do it? Please suggest....

2 Answers 2

1

you will need an SDK to save as the xlsx format. I dont know where to get the openxml sdk to do it, but here is a code snippet to save as a CSV which can be opened in excel as well.

List<Person> persons; // populated earlier
using(StreamWriter wr = new StreamWriter("myfile.csv"))
{
   foreach(Person person in persons)
   {
     wr.WriteLine(person.Name + "," + person.City + "," + person.Age);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Run through your list with a foreach loop, and create a CSV file, one line per person. CSV files can be opened directly by Excel.

Comments

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.