Most Popular

Thứ Năm, 28 tháng 5, 2015

Export dữ liệu excel MVC Asp.Net

Bước 1 :  VisualStudio>>NewProject >> Đặt tên là 'Export' 

Bước2 :Tạo database và Table

Thay đổi webconfig 

<connectionstrings>
    <add name="Leedhar_Export" connectionstring="Data Source=LEEDHAR2-PC\SQLEXPRESS;Database=Leedhar_Export;Trusted_Connection=true;Persist Security Info=True" providername="System.Data.SqlClient"></add>  </connectionstrings>


Tạo database và lớp model Leedhar_Export 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Export.Models
{
    public class Leedhar_Export:DbContext
    {

        public DbSet Studentrecord { get; set; }
    }
}




Sau đó thêm code cho thuộc tính cho lớp Studentrecord 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace Export.Models
{
   public class StudentRecord
    {

        [Key]
        public virtual int id { get; set; }
        [Required]
        public virtual String Name { get; set; }
        public virtual String Marks { get; set; }
        public virtual String Grade { get; set; }
    }
}


Bước 3 :Tạo View Page 

Thêm 1 Controller có action là StudentDetails

  public ActionResult StudentDetails()
        {
            var model = db.Studentrecord;
            return View(model);
           
        }



Then right click on the StudentDetails and add view page and give the master page and also give the link for student details in the master page. 

Next Come to the view page. Add following code 

@model IEnumerable<Export.Models.StudentRecord>
@{
    ViewBag.Title = "StudentDetails";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>StudentDetails</h2>
 @using (Html.BeginForm("addstudentdetails", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
     
     <table>
     <tr><td>Name</td><td><input type="text" name="Name" id="Name" /></td></tr>
      <tr><td>Marks</td><td><input type="text" name="Marks" id="Marks" /></td></tr>
        <tr><td>Grade</td><td><input type="text" name="Grade" id="Grade" /></td></tr>
      <tr><td></td><td><input  type="submit" name="save" id="save" value="save"/></td></tr>
     </table>
 }


 <table>
    <tr><th>
           Name
        </th><th>
          Marks
        </th>
        <th>Grade</th>
        </tr>
        @foreach (var item in Model)
        {
<tr><td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
     <td>
            @Html.DisplayFor(modelItem => item.Marks)
        </td>   
         <td>
            @Html.DisplayFor(modelItem => item.Grade)
        </td>  
        </tr>
        }
        </table>
         @using (Html.BeginForm("ExportData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
         {
        <table>
         <tr><td></td><td><input  type="submit" name="Export" id="Export" value="Export"/></td></tr>
        
        </table>
         }



Then for save the details entered add following code to the Home Controller. 

 public ActionResult addstudentdetails(string Name, string Marks, string Grade)
        {

            db.Studentrecord.Add(new StudentRecord() { Name= Name, Marks= Marks, Grade= Grade });
            db.SaveChanges();
            return RedirectToAction("StudentDetails");
        }




Then for export the data to excel add following code to the Home Controller 

 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");
        }



Bước 5 :The final output
                       DOWNLOAD SOURCE CODE 

Export Data to Excel using Linq.zip

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 nhận xét

 
© 2011 Share Tài Liệu
Posts RSSComments RSS
Back to top