Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
model.cs

C#
public class Results
{
     List<string>name{get;set;}
     List<string>phone{get;set;}
     public string id {get;set;}
 }




if i have list like this
C#
List<Results> lst = new List<Results>();




how can i export elements of "lst" to excel

What I have tried:

C#
[HttpPost]
   public ActionResult ShowResult()
   {

       GridView gv = new GridView();
       List<SearchResults> lst = (List<SearchResults>)TempData["lst"];
       gv.DataSource = lst;
       gv.DataBind();
       Response.ClearContent();
       Response.Buffer = true;
       Response.AddHeader("content-disposition",       "attachment;filename=Marklist.xls");
       Response.ContentType = "application/ms-excel";
       Response.Charset = "";
       StringWriter sw = new StringWriter();
       HtmlTextWriter htw = new HtmlTextWriter(sw);
       gv.RenderControl(htw);
       Response.Output.Write(sw.ToString());
       Response.Flush();
       Response.End();
       return View(model);
   }
Posted
Updated 2-May-16 7:32am
v2
Comments
Ehsan Sajjad 2-May-16 7:18am    
there are third party libraries out which can be used for this
Karthik_Mahalingam 2-May-16 14:19pm    
show the sample of data.

1 solution

You need to convert your List to a datatable, then you can export that datatable to an Excel file.
Follow this one:
Exporting Generic List<T> to Excel in C# Using Interop[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900