Export data to Excel MVC 5 c# asp.net [Code]

Thảo luận trong 'Lập Trình Website MVC5 & MVC6' bắt đầu bởi admin, 8/1/19.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,905
    Đã được thích:
    1,199
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Code Export data to Excel MVC 5 c# asp.net cho bạn nào cần sử dụng xuất dữ liệu từ database ra ngoài excel sử dụng linq, model trong MVC 5 đã test qua rất êm. Code cho phép bạn cấu hình color màu sắc header, màu chữ color của file Header khi Export ra ngoài. File định dạng xls

    export-data-to-excel-mvc-linq-model.jpg

    Code tại Model
    Mã:
    namespace MyNamesPace.Models
    {
        using System;
        using System.Collections.Generic;
        using System.Web.Mvc;
    
        public partial class MyDataModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public Nullable<System.DateTime> CreateTime { get; set; } 
        }
    }
    Code tại Controller
    Mã:
    public ActionResult Index()
    {
        var model = MyDataModel
            .OrderByDescending(p => p.CreateTime)
            .ToList();
        return View(model);
    }
    [HttpPost]
    public ActionResult ExportToExcel()
    {
        var gv = new GridView();
        gv.DataSource = this.dbc.MyDataModel
            .Where(p => p.Activate == true)
            .Select(r => new {
                Names = r.Name,
                Emails = r.Email,                   
                CreateTimes = r.CreateTime
            })
            .OrderByDescending(p => p.CreateTimes)
            .ToList();
        gv.DataBind();
        Response.Clear();
        Response.Buffer = true;
        //Response.AddHeader("content-disposition",
        // "attachment;filename=GridViewExport.xls");
        Response.Charset = "utf-8";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        //Mã hóa chữa sang UTF8
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
    
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
    
        for (int i = 0; i < gv.Rows.Count; i++)
        {
            //Apply text style to each Row
            gv.Rows[i].Attributes.Add("class", "textmode");
        }
        //Add màu nền cho header của file excel
        gv.HeaderRow.BackColor= System.Drawing.Color.DarkBlue;
        //Màu chữ cho header của file excel
        gv.HeaderStyle.ForeColor = System.Drawing.Color.White;
    
        gv.HeaderRow.Cells[0].Text = "Tên";
        gv.HeaderRow.Cells[1].Text = "Email";
        gv.HeaderRow.Cells[2].Text = "Ngày tạo";
        gv.RenderControl(hw);
    
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
        var model = MyDataModel
            .OrderByDescending(p => p.CreateTime)
            .ToList();
        return View("View",model);
    }
    
    Tại View index.cshtml
    HTML:
    @using (Html.BeginForm("ExportToExcel", "ControllerName", FormMethod.Post))
    {
        <input type="submit" value="Xuất File Excel" class="button" />
    }
    
     
    Cảm ơn đã xem bài:

    Export data to Excel MVC 5 c# asp.net [Code]



Chủ để tương tự : Export data
Diễn đàn Tiêu đề Date
Lập Trình Website MVC5 & MVC6 There is already an open DataReader associated with this Command which must be closed first 21/5/21
Lập Trình Website MVC5 & MVC6 Lỗi: There is already an open DataReader associated with this Command which must be closed first 30/10/20
Lập Trình Website MVC5 & MVC6 Nối 2 ToList() data trong mvc, how to add (merge) two list in mvc 27/12/19
Lập Trình Website MVC5 & MVC6 Update list data using linq trong MVC C# Asp.net như thế nào? 14/1/19
Lập Trình Website MVC5 & MVC6 Add Multiple Foreign Key for the Same table in MVC Database First SQL 20/10/18