Description:-
In previous
Example we Explain that How to Export Gridview data to Excelsheet in Asp.Net
Export Gridview data to excelsheet in asp.net but now in this example we explain that how to Export Gridview Data to Excel
sheet in MVC4.
This is very useful when we have to generally Create a Report or you can Directally print the Document
you have to set Content Type of the page Before sending or Exporting data to ExcelSheet like
Response.ContentType
= "application/ms-excel";
to show example of WCF service for insert,update,delete CRUD operation using WCF service
check the Extention of file when upload upload only image or word file
there are number of steps to follow how to Export or transfer Gridview or webgird data to Excelsheet Database file.
step 1:- First create a LinqtoSQL class in Model folder and drag and srop the table that you have to use in example.
step 2:- create a controller like exportgridtoexcel in controller Folder.
step 3:- create a view page For display the details we have to create a view page for that in the exportgridtoexcel controller add following code.
DataClasses1DataContext dc = new DataClasses1DataContext();
public ActionResult Index()
{
var lst = (from p in dc.movies select p).ToList();
return View(lst);
}
step 4:-Then for export the data to excel add following code to the exportgridtoexcel Controller
public List<movie>
fetch()
{
var lst = (from p in dc.movies select p);
return lst.ToList();
}
public ActionResult ExportData()
{
GridView gv = new GridView();
gv.DataSource =fetch();
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("Index");
}