tags:

views:

47

answers:

2

Hi All,

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

+1  A: 

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);
   }
}
Andrew Keith
A: 

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.

Robert Harvey