In this article I will show you how to export student detail from MVC 4 application using Entity-framework. Let start with creating simple MVC 4 project.
1. Open Visual Studio 2010 => New Project => Select ASP.Net MVC4 web Application => Name it Export => Click ok
2. Select Internet Application click OK.
3. Now add 2 class file name ExportDB and StudenDetail respectively and add code mention below
public class Export_Excel: DbContext { public DbSet<StudentDetail> Studentrecord{ get; set; } } public class StudentDetail { [Key] public int id { get; set; } [Required] public String Name { get; set; } public String Address { get; set; } public String Marks { get; set; } }
4. Now add right click on controller => Add => Controller.
5. Put controller name “StudentDetail”, model class StudentDetail and data context class “Export_Excel” then click on Add button. It will create one controller file and 5 cshtml file for add/edit/delete.
6. Run application by F5 and in your URL type studentdetail if cause error then type studentdetail/create otherwise click on create new. Add data from here.
7. After adding records from application. Add following code in studentdetailcontroller
public ActionResult ExportData() { GridView gv = new GridView(); gv.DataSource = db.Studentrecord.ToList(); 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 RedirectToAction("StudentDetails"); }
8. Add following code in index.cshtml
@using (Html.BeginForm("ExportData", "Studentdetail", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td></td>
<td>
<input type="submit" name="Export" id="Export" value="Export" /></td>
</tr>
</table>
}
9. Now run application and append studentdetail on URL and click on export button.
This will export you data into excel. For code check this: click
Related articles
- LINQ Include command mvc3 and webgrid (stackoverflow.com)
- Getting started with MVC 4 (dotprogramming.blogspot.com)
- Basics of MVC (kirangondi.wordpress.com)
- MVC4 Export Data to Excel using Linq
Hi,
On the same line can you share code to export data in pdf without any 3rd party tool.
Thanks.
Regards,
Awais
Thanks Awais for your comment, I will to update that asap but with some open source class.
Awesome….
Thank you So Much SATISHRATNAPARKHI, it’s worked excelent.
but I have a situation, i have a search option, and when I try to export the result, it’s bringing all the data, not my search result.
How can i do that
thank you in Advance
Hi Carlos, In your case you have to write two method one is for search results and another one is for full results. If you need more detail then let me know?
how to import contacts from gmail using mvc 4
I am not sure about this, as I am working on MVC 5.
works like a charm. Thanks
Excellent but i want to export button to the table we can export that table to excel