Click here to Skip to main content
15,903,760 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Take the Data of “GRIDVIEW” to Excel. The data is in ItemsSource as AnonymousType, Table or DataView

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
9 May 2013CPOL2 min read 8.4K   4  
Taking data of Gridview to Excel

Introduction

The data of enumerable can be sited or bandied to ItemsSource of GRIDVIEW in WPF or Silverlight. So GRIDVIEW can dynamically show data according to different data of enumerable.
We can take the data to Excel with a special format. To take DataTable to Excel is easy. In here, I have not given much detail. If you want to see the code, you can view my demo.

Background

The data of enumerable can be from TABLE, DATAVIEW or list of “AnonymousType”. DATAVIEW also easily converts to DATATABLE. When you use LINQ to create new data, you get the list of “AnonymousType
e.g.

C#
var qqq = setss.Employees.
            Select(p => new { ww = p.FirstName.Replace("A", " "), we = p.LastName, p.BirthDate });
            gridview_1.ItemsSource = qqq.ToList();

In here, the “gridview_1.ItemsSource“ contains list of “AnonymousType”. We create the method ”Anony2tab” who converts list of “AnonymousType” to table.

Using the Code

The following code converts the data of ItemsSource to DATATABLE and then creates Excel by table data. The method tab2excel converts the datatable to Excel.

Three buttons create type of enumerable data:

Table

C#
var qqq = setss.Employees ;
gridview_1.ItemsSource = qqq;

VIEW

C#
var d_v = setss.Employees.AsDataView();
d_v.RowFilter = "LastName like '%e%'";
d_v.Sort = "Title DESC";
gridview_1.ItemsSource = d_v;

LINQ NEW

C#
var qqq = setss.Employees.
Select(p => new { ww = p.FirstName.Replace("A", " "), we = p.LastName, p.BirthDate });
gridview_1.ItemsSource = qqq.ToList();

The following code is used to convert them to table:

C#
gd2excel g2e = new gd2excel();
            var output_name = System.Environment.CurrentDirectory + @"\aaa.xls";
            if (File.Exists(output_name)) File.Delete(output_name);
            DataTable tb = new DataTable();
            if (gridview_1.ItemsSource.GetType().IsSubclassOf(typeof(DataTable)))
            {
                //  is table 
                tb = (DataTable)gridview_1.ItemsSource;
            }
            else if (gridview_1.ItemsSource.GetType().Name == "List`1")
            {
                // call the mothed Anony2tab
                tb = g2e.Anony2tab(gridview_1.ItemsSource);

            }
            else if (gridview_1.ItemsSource.GetType().IsSubclassOf(typeof(DataView)))
            {
                // convert 
                var qq = (DataView)gridview_1.ItemsSource;
                tb = qq.ToTable();
            }
            else
            {
                MessageBox.Show("for type...  " + gridview_1.ItemsSource.GetType().Name);
                return;
                 
            }
            g2e.TableToExcel(tb, output_name);

from AnonymousType to table:

C#
public DataTable Anony2tab(System.Collections.IEnumerable gd)
        {
            var tb = new DataTable(); 
            foreach (var r_c in gd)
            {
                var prop_s = r_c.GetType().GetProperties();
                if (tb.Columns.Count==0)
                {
                 foreach (var prop in prop_s)
                 {
                     var t_type = prop.PropertyType;
                     var t_name = prop.Name;
                     tb.Columns.Add(t_name, t_type);
                 }
                }
                var n_row = tb.NewRow();
                foreach (var prop in prop_s)
                {
                     var t_name = prop.Name;
                     var t_value = prop.GetValue(r_c, null);
                   
                    n_row[t_name] = t_value;
                }
                tb.Rows.Add(n_row);
            }
            return tb;
        }

AnonymousType” is the list which contains different type (class). GetType() gets one class, the class contains method, property and so on. So each of the values respond to its type and name.

Creates an empty table "TB" and then reads enumerable for each list, and puts them on the table. In reading the first list, it is to establish "TB". "tb.Columns.Count==0" is said that TB is not created.
T_type is the data type.
T_name gets the name of value, we use this name as name of column.

T_value is the data value.
n_row[t_name] = t_value; assigns a value to a column with this name.

So you get the new table.t.

"TableToExcel" is a table into Excel methods. There are many examples on the internet. You can also reference the code in my demo here.

The demo is in WPF C# VS2010 with dataset, not connected with any database.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --