NEWS

[DEVEXPRESS] Hướng dẫn xuất dữ liệu nhiều Gridview ra một file Excel winform

[DEVEXPRESS] Hướng dẫn xuất dữ liệu nhiều Gridview ra một file Excel winform
Đăng bởi: Thảo Meo - Lượt xem: 6878 20:43:05, 25/10/2021C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách xuất dữ liệu từ nhiều Gridview ra một file excel trên Devexpress C#, Winform.

[DEVEXPRESS] How to Export Multi Gridview To one file excel C#

Bình thường, trên GridControl công cụ của Devexpress, có hỗ trợ các bạn cách xuất dữ liệu ra file tập tin Excel một cách dễ dàng.

Tuy nhiên, nếu bạn có nhiều GridControl, và các bạn muốn xuất dữ liệu mỗi GridView trên từng Sheet một trên File excel thì cách bạn làm thế nào.

Giao diện demo ứng dụng Export Multi Gridview to File Excel c#:

export_excel_multi_csharp

Ở hình demo trên của mình, các bạn thấy mình có ba GridControl, vào khi bấm vào nút Export Excel thì sẽ xuất dữ liệu ba gridcontrol này ra một file excel.

Để làm được chúng ta sẽ sử dụng compositeLink để kết nối các GridControl này lại với nhau.

Chi tiết thực hiện mình có quay video hướng dẫn các bạn từng bước thực hiện.

Source code C#:

using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExportMultiGridViewToOneFileExcel
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

     
        private void Form1_Load(object sender, EventArgs e)
        {
            var table1 = new DataTable();
            table1.Columns.Add("id");
            table1.Columns.Add("name");

            table1.Rows.Add("1", "Nguyễn Thảo");
            table1.Rows.Add("2", "Nguyễn Phương Nhi");
            table1.Rows.Add("3", "Cái Trí MInh");
            table1.Rows.Add("4", "Nguyễn Đình Tuyên");
            table1.Rows.Add("5", "Trịnh Quốc Khang");
            table1.Rows.Add("6", "Nguyễn Thị Cẩm Tú");
            table1.Rows.Add("7", "Hoang Thị Thảo");

            gridControl1.DataSource = table1;

            // table 2 
            var table2 = new DataTable();
            table2.Columns.Add("id");
            table2.Columns.Add("foodname");

            table2.Rows.Add("1", "Hải sản xào");
            table2.Rows.Add("2", "Éch chiên bột");
            table2.Rows.Add("3", "Dồi trường hấp gừng");        

            gridControl2.DataSource = table2;

            //table 3
  
            var table3 = new DataTable();
            table3.Columns.Add("id");
            table3.Columns.Add("subject");

            table3.Rows.Add("1", "Toán học");
            table3.Rows.Add("2", "Hóa học");
            table3.Rows.Add("3", "Vật lý");
            table3.Rows.Add("4", "Tiếng Anh");
            table3.Rows.Add("5", "Văn Học");

            gridControl3.DataSource = table3;


        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            gridControl1.ForceInitialize();
            gridControl2.ForceInitialize();
            gridControl3.ForceInitialize();
            compositeLink.CreatePageForEachLink();
            XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions();
            options.ExportMode = XlsxExportMode.SingleFilePageByPage;
            compositeLink.ExportToXlsx("laptrinhvb.xlsx", options);
            Process.Start("laptrinhvb.xlsx");
        }
    }
}

Video hướng dẫn step by step:

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn xuất dữ liệu nhiều Gridview ra một file Excel winform
Đăng bởi: Thảo Meo - Lượt xem: 6878 20:43:05, 25/10/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.