Export Data From Data Grid To Excel in Silverlight

I.Introduction

Trong bài này mình xin chia sẽ cách Load dữ liệu vào DataGrid và từ đó Xuất nó ra file Excels.

II.Creating Project and Coding

Mở Visual Studio 2010 và tạo Silverlight Application

image

Tiếp theo chúng ta Tạo giao diện cho Ứng dụng gồm 1 DataGrid và một Button

image

các bạn vào Code XAML của MainPage và tìm thẻ Button để tạo sự kiện Click cho Button.

image

Chúng ta có Code XAML của giao diện như sau :

   1: <UserControl x:Class="ExportDatatoExcel.MainPage"

   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

   5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   6:     mc:Ignorable="d"

   7:     d:DesignHeight="300" d:DesignWidth="400" 

   8:     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

   9:  

  10:     <Grid x:Name="LayoutRoot">

  11:         <Grid.RowDefinitions>

  12:             <RowDefinition Height="*"/>

  13:             <RowDefinition Height="50"/>

  14:         </Grid.RowDefinitions>

  15:         <sdk:DataGrid AutoGenerateColumns="True"  Margin="12,12,12,43" Name="dataGrid" />

  16:         <Button Content="Export to Excel"  Grid.Row="1" FontSize="20" Name="btn_exporttoExcel" Click=""/>

  17:         <Grid.Background>

  18:             <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

  19:                 <GradientStop Color="Black" Offset="0" />

  20:                 <GradientStop Color="White" Offset="1" />

  21:             </LinearGradientBrush>

  22:         </Grid.Background>

  23:     </Grid>

  24: </UserControl>

tiếp theo chúng ta Add thêm thư viện Microsoft.SCharp vào Project –> các bạn chọn Add Reference

image

Add thư viện Microsoft.Csharp vào :

image

Để Export ra files Excel một cách đơn giản, và tránh các thủ tục rờm rà, chúng ta chạy Silverlight ở Local. Các bạn chọn Right Click vào Project –> properties

image

Khi cửa sổ Properties hiện ra các bạn check vào Enable running application out of browser

và click vào Out-of-Browser Setting

image

Check vào Require elevated trust when running out of browser

image

Tiếp theo chúng ta tạo một CSDL đơn giản :

   1: public class Student

   2: {

   3:     public string Name { get; set; }

   4:     public int Age { get; set; }

   5:     public string Email { get; set; }

   6:     public string phone { get; set; }

   7:     public List<Student> getData()

   8:     {

   9:         List<Student> Students = new List<Student>

  10:         {

  11:             new Student{Name = "Pham Phuong Nguyen", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  12:             new Student{Name = "Binh Nguyen", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  13:             new Student{Name = "Phi Diep", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  14:             new Student{Name = "Chi Khang", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  15:             new Student{Name = "Minh Phuc", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  16:             new Student{Name = "Quang Huy", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  17:             new Student{Name = "Minh Sang", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  18:             new Student{Name = "Ngoc Hien", Age = 22, Email = "pham.nguyen@hotmail.com", phone = "01699942228"},

  19:                 

  20:         };

  21:         return Students;

  22:     }

Các bạn vào MainPage.xaml.cs để thử Load dữ liệu ra ngoài

   1: public MainPage()

   2: {

   3:     InitializeComponent();

   4:     Student student = new Student();

   5:     dataGrid.ItemsSource = student.getData();

   6: }

Cuối cùng chúng ta vào Phương thức Click để xử lý cho việc Export to Excel:

   1: private void btn_exporttoExcel_Click(object sender, RoutedEventArgs e)

   2:  

   3:    dynamic ExcelApp;

   4:    ExcelApp = AutomationFactory.CreateObject("Excel.Application");

   5:    ExcelApp.Visible = true;

   6:    dynamic WorkBook = ExcelApp.workbooks();

   7:    WorkBook.Add();

   8:    dynamic Sheet = ExcelApp.ActiveSheet();

   9:    dynamic cell = null;

  10:    int index = 1;

  11:    foreach (Student student in dataGrid.ItemsSource)

  12:    {

  13:        cell = Sheet.Cells[index, 1];

  14:        cell.Value = student.Name;

  15:        cell = Sheet.Cells[index, 2];

  16:        cell.Value = student.Age;

  17:        cell = Sheet.Cells[index, 3];

  18:        cell.Value = student.phone;

  19:        cell = Sheet.Cells[index, 4];

  20:        cell.Value = student.Email;

  21:        index++;

  22:    }

  23:  

Run Application để xem kết quả :

image

khi các bạn Click vào Export to Excel:

image

Full Source:

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Net;

   5: using System.Windows;

   6: using System.Windows.Controls;

   7: using System.Windows.Documents;

   8: using System.Windows.Input;

   9: using System.Windows.Media;

  10: using System.Windows.Media.Animation;

  11: using System.Windows.Shapes;

  12: using System.Runtime.InteropServices.Automation;

  13:  

  14: namespace ExportDatatoExcel

  15: {

  16:     public partial class MainPage : UserControl

  17:     {

  18:         public MainPage()

  19:         {

  20:             InitializeComponent();

  21:             Student student = new Student();

  22:             dataGrid.ItemsSource = student.getData();

  23:         }

  24:  

  25:         private void btn_exporttoExcel_Click(object sender, RoutedEventArgs e)

  26:         {

  27:             dynamic ExcelApp;

  28:             ExcelApp = AutomationFactory.CreateObject("Excel.Application");

  29:             ExcelApp.Visible = true;

  30:             dynamic WorkBook = ExcelApp.workbooks();

  31:             WorkBook.Add();

  32:             dynamic Sheet = ExcelApp.ActiveSheet();

  33:             dynamic cell = null;

  34:             int index = 1;

  35:             foreach (Student student in dataGrid.ItemsSource)

  36:             {

  37:                 cell = Sheet.Cells[index, 1];

  38:                 cell.Value = student.Name;

  39:                 cell = Sheet.Cells[index, 2];

  40:                 cell.Value = student.Age;

  41:                 cell = Sheet.Cells[index, 3];

  42:                 cell.Value = student.phone;

  43:                 cell = Sheet.Cells[index, 4];

  44:                 cell.Value = student.Email;

  45:                 index++;

  46:             }

  47:  

  48:         }

  49:     }

  50: }

/*Web is beautiful*/

Source Code Download Here !

Leave a comment