using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.IO;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            Random rnd = new Random();

            DataContext = (from i in Enumerable.Range(0, 100)
                         select new MyObject()
                         {
                             Column1 = i,
                             Column2 = rnd.Next(int.MinValue, int.MaxValue),
                             Column3 = rnd.Next(int.MinValue, int.MaxValue),
                             Column4 = rnd.Next(int.MinValue, int.MaxValue),
                             Column5 = rnd.Next(int.MinValue, int.MaxValue),
                             Column6 = rnd.Next(int.MinValue, int.MaxValue),
                             Column7 = rnd.Next(int.MinValue, int.MaxValue),
                             Column8 = rnd.Next(int.MinValue, int.MaxValue),
                             Column9 = rnd.Next(int.MinValue, int.MaxValue),
                             Column10 = rnd.Next(int.MinValue, int.MaxValue),
                             Column11 = rnd.Next(int.MinValue, int.MaxValue),
                             Column12 = rnd.Next(int.MinValue, int.MaxValue),
                             Column13 = rnd.Next(int.MinValue, int.MaxValue),
                             Column14 = rnd.Next(int.MinValue, int.MaxValue),
                             Column15 = rnd.Next(int.MinValue, int.MaxValue),
                             Column16 = rnd.Next(int.MinValue, int.MaxValue),
                             Column17 = rnd.Next(int.MinValue, int.MaxValue),
                             Column18 = rnd.Next(int.MinValue, int.MaxValue),
                             Column19 = rnd.Next(int.MinValue, int.MaxValue),
                             Column20 = rnd.Next(int.MinValue, int.MaxValue),
                             Column21 = rnd.Next(int.MinValue, int.MaxValue),
                             Column22 = rnd.Next(int.MinValue, int.MaxValue),
                             Column23 = rnd.Next(int.MinValue, int.MaxValue),
                             Column24 = rnd.Next(int.MinValue, int.MaxValue),
                             Column25 = rnd.Next(int.MinValue, int.MaxValue),
                             Column26 = rnd.Next(int.MinValue, int.MaxValue),
                             Column27 = rnd.Next(int.MinValue, int.MaxValue),
                             Column28 = rnd.Next(int.MinValue, int.MaxValue),
                             Column29 = rnd.Next(int.MinValue, int.MaxValue),
                             Column30 = rnd.Next(int.MinValue, int.MaxValue),
                             Column31 = rnd.Next(int.MinValue, int.MaxValue),
                             Column32 = rnd.Next(int.MinValue, int.MaxValue),
                             Column33 = rnd.Next(int.MinValue, int.MaxValue),
                             Column34 = rnd.Next(int.MinValue, int.MaxValue),
                             Column35 = rnd.Next(int.MinValue, int.MaxValue),
                             Column36 = rnd.Next(int.MinValue, int.MaxValue),
                             Column37 = rnd.Next(int.MinValue, int.MaxValue),
                             Column38 = rnd.Next(int.MinValue, int.MaxValue),
                             Column39 = rnd.Next(int.MinValue, int.MaxValue),
                             Column40 = rnd.Next(int.MinValue, int.MaxValue),
                             Column41 = rnd.Next(int.MinValue, int.MaxValue),
                             Column42 = rnd.Next(int.MinValue, int.MaxValue),
                             Column43 = rnd.Next(int.MinValue, int.MaxValue),
                             Column44 = rnd.Next(int.MinValue, int.MaxValue),
                             Column45 = rnd.Next(int.MinValue, int.MaxValue),
                             Column46 = rnd.Next(int.MinValue, int.MaxValue),
                             Column47 = rnd.Next(int.MinValue, int.MaxValue),
                             Column48 = rnd.Next(int.MinValue, int.MaxValue),
                             Column49 = rnd.Next(int.MinValue, int.MaxValue),
                             Column50 = rnd.Next(int.MinValue, int.MaxValue),
                             Column51 = rnd.Next(int.MinValue, int.MaxValue),
                             Column52 = rnd.Next(int.MinValue, int.MaxValue),
                             Column53 = rnd.Next(int.MinValue, int.MaxValue),
                             Column54 = rnd.Next(int.MinValue, int.MaxValue),
                             Column55 = rnd.Next(int.MinValue, int.MaxValue),
                             Column56 = rnd.Next(int.MinValue, int.MaxValue),
                             Column57 = rnd.Next(int.MinValue, int.MaxValue),
                             Column58 = rnd.Next(int.MinValue, int.MaxValue),
                             Column59 = rnd.Next(int.MinValue, int.MaxValue),
                             Column60 = rnd.Next(int.MinValue, int.MaxValue),
                             Column61 = rnd.Next(int.MinValue, int.MaxValue),
                             Column62 = rnd.Next(int.MinValue, int.MaxValue),
                             Column63 = rnd.Next(int.MinValue, int.MaxValue),
                             Column64 = rnd.Next(int.MinValue, int.MaxValue),
                             Column65 = rnd.Next(int.MinValue, int.MaxValue),
                             Column66 = rnd.Next(int.MinValue, int.MaxValue),
                             Column67 = rnd.Next(int.MinValue, int.MaxValue),
                             Column68 = rnd.Next(int.MinValue, int.MaxValue),
                             Column69 = rnd.Next(int.MinValue, int.MaxValue),
                             Column70 = rnd.Next(int.MinValue, int.MaxValue),
                             Column71 = rnd.Next(int.MinValue, int.MaxValue),
                             Column72 = rnd.Next(int.MinValue, int.MaxValue),
                             Column73 = rnd.Next(int.MinValue, int.MaxValue),
                             Column74 = rnd.Next(int.MinValue, int.MaxValue),
                             Column75 = rnd.Next(int.MinValue, int.MaxValue),
                             Column76 = rnd.Next(int.MinValue, int.MaxValue),
                             Column77 = rnd.Next(int.MinValue, int.MaxValue),
                             Column78 = rnd.Next(int.MinValue, int.MaxValue),
                             Column79 = rnd.Next(int.MinValue, int.MaxValue),
                             Column80 = rnd.Next(int.MinValue, int.MaxValue),
                             Column81 = rnd.Next(int.MinValue, int.MaxValue),
                             Column82 = rnd.Next(int.MinValue, int.MaxValue),
                             Column83 = rnd.Next(int.MinValue, int.MaxValue),
                             Column84 = rnd.Next(int.MinValue, int.MaxValue),
                             Column85 = rnd.Next(int.MinValue, int.MaxValue),
                             Column86 = rnd.Next(int.MinValue, int.MaxValue),
                             Column87 = rnd.Next(int.MinValue, int.MaxValue),
                             Column88 = rnd.Next(int.MinValue, int.MaxValue),
                             Column89 = rnd.Next(int.MinValue, int.MaxValue),
                             Column90 = rnd.Next(int.MinValue, int.MaxValue),
                             Column91 = rnd.Next(int.MinValue, int.MaxValue),
                             Column92 = rnd.Next(int.MinValue, int.MaxValue),
                             Column93 = rnd.Next(int.MinValue, int.MaxValue),
                             Column94 = rnd.Next(int.MinValue, int.MaxValue),
                             Column95 = rnd.Next(int.MinValue, int.MaxValue),
                             Column96 = rnd.Next(int.MinValue, int.MaxValue),
                             Column97 = rnd.Next(int.MinValue, int.MaxValue),
                             Column98 = rnd.Next(int.MinValue, int.MaxValue),
                             Column99 = rnd.Next(int.MinValue, int.MaxValue),
                             Column100 = rnd.Next(int.MinValue, int.MaxValue)
                         }).ToList();
        }

        static void grid_Exporting(object sender, GridViewExportEventArgs e)
        {
            if (e.Element == ExportElement.HeaderRow || e.Element == ExportElement.FooterRow
                || e.Element == ExportElement.GroupFooterRow)
            {
                e.Height = 25;
                e.FontSize = 20;
                e.FontWeight = FontWeights.Bold;
            }
            else if (e.Element == ExportElement.Row)
            {
                e.Height = 15;
            }
            else if (e.Element == ExportElement.GroupHeaderRow)
            {
                e.FontFamily = new FontFamily("Verdana");
                e.Background = Colors.LightGray;
                e.Height = 30;
            }

            (sender as RadGridView).Exporting -= grid_Exporting;
        } 

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog()
            {
                DefaultExt = "xls",
                Filter = "Excel files (*.xls)|*.xls",
                FilterIndex = 1
            };

            try
            {
                grid.Exporting += new EventHandler<GridViewExportEventArgs>(grid_Exporting);

                if (dialog.ShowDialog() == true)
                {
                    using (Stream stream = dialog.OpenFile())
                    {

                        grid.Export(stream, new Telerik.Windows.Controls.GridViewExportOptions()
                        {
                            Format = Telerik.Windows.Controls.ExportFormat.ExcelML,
                            ShowColumnHeaders = true
                        });

                    }
                }

            }
            catch (IOException)
            {
                MessageBox.Show("File with the current name is open. Please rename or close the open file", "Error", MessageBoxButton.OK);
            }            
        }
    }

    public class MyObject
    {
        private int column1;
        private int column2;
        private int column3;
        private int column4;
        private int column5;
        private int column6;
        private int column7;
        private int column8;
        private int column9;
        private int column10;
        private int column11;
        private int column12;
        private int column13;
        private int column14;
        private int column15;
        private int column16;
        private int column17;
        private int column18;
        private int column19;
        private int column20;
        private int column21;
        private int column22;
        private int column23;
        private int column24;
        private int column25;
        private int column26;
        private int column27;
        private int column28;
        private int column29;
        private int column30;
        private int column31;
        private int column32;
        private int column33;
        private int column34;
        private int column35;
        private int column36;
        private int column37;
        private int column38;
        private int column39;
        private int column40;
        private int column41;
        private int column42;
        private int column43;
        private int column44;
        private int column45;
        private int column46;
        private int column47;
        private int column48;
        private int column49;
        private int column50;
        private int column51;
        private int column52;
        private int column53;
        private int column54;
        private int column55;
        private int column56;
        private int column57;
        private int column58;
        private int column59;
        private int column60;
        private int column61;
        private int column62;
        private int column63;
        private int column64;
        private int column65;
        private int column66;
        private int column67;
        private int column68;
        private int column69;
        private int column70;
        private int column71;
        private int column72;
        private int column73;
        private int column74;
        private int column75;
        private int column76;
        private int column77;
        private int column78;
        private int column79;
        private int column80;
        private int column81;
        private int column82;
        private int column83;
        private int column84;
        private int column85;
        private int column86;
        private int column87;
        private int column88;
        private int column89;
        private int column90;
        private int column91;
        private int column92;
        private int column93;
        private int column94;
        private int column95;
        private int column96;
        private int column97;
        private int column98;
        private int column99;
        private int column100;

        public int Column1 { get { return this.column1; } set { this.column1 = value; } }
        public int Column2 { get { return this.column2; } set { this.column2 = value; } }
        public int Column3 { get { return this.column3; } set { this.column3 = value; } }
        public int Column4 { get { return this.column4; } set { this.column4 = value; } }
        public int Column5 { get { return this.column5; } set { this.column5 = value; } }
        public int Column6 { get { return this.column6; } set { this.column6 = value; } }
        public int Column7 { get { return this.column7; } set { this.column7 = value; } }
        public int Column8 { get { return this.column8; } set { this.column8 = value; } }
        public int Column9 { get { return this.column9; } set { this.column9 = value; } }
        public int Column10 { get { return this.column10; } set { this.column10 = value; } }
        public int Column11 { get { return this.column11; } set { this.column11 = value; } }
        public int Column12 { get { return this.column12; } set { this.column12 = value; } }
        public int Column13 { get { return this.column13; } set { this.column13 = value; } }
        public int Column14 { get { return this.column14; } set { this.column14 = value; } }
        public int Column15 { get { return this.column15; } set { this.column15 = value; } }
        public int Column16 { get { return this.column16; } set { this.column16 = value; } }
        public int Column17 { get { return this.column17; } set { this.column17 = value; } }
        public int Column18 { get { return this.column18; } set { this.column18 = value; } }
        public int Column19 { get { return this.column19; } set { this.column19 = value; } }
        public int Column20 { get { return this.column20; } set { this.column20 = value; } }
        public int Column21 { get { return this.column21; } set { this.column21 = value; } }
        public int Column22 { get { return this.column22; } set { this.column22 = value; } }
        public int Column23 { get { return this.column23; } set { this.column23 = value; } }
        public int Column24 { get { return this.column24; } set { this.column24 = value; } }
        public int Column25 { get { return this.column25; } set { this.column25 = value; } }
        public int Column26 { get { return this.column26; } set { this.column26 = value; } }
        public int Column27 { get { return this.column27; } set { this.column27 = value; } }
        public int Column28 { get { return this.column28; } set { this.column28 = value; } }
        public int Column29 { get { return this.column29; } set { this.column29 = value; } }
        public int Column30 { get { return this.column30; } set { this.column30 = value; } }
        public int Column31 { get { return this.column31; } set { this.column31 = value; } }
        public int Column32 { get { return this.column32; } set { this.column32 = value; } }
        public int Column33 { get { return this.column33; } set { this.column33 = value; } }
        public int Column34 { get { return this.column34; } set { this.column34 = value; } }
        public int Column35 { get { return this.column35; } set { this.column35 = value; } }
        public int Column36 { get { return this.column36; } set { this.column36 = value; } }
        public int Column37 { get { return this.column37; } set { this.column37 = value; } }
        public int Column38 { get { return this.column38; } set { this.column38 = value; } }
        public int Column39 { get { return this.column39; } set { this.column39 = value; } }
        public int Column40 { get { return this.column40; } set { this.column40 = value; } }
        public int Column41 { get { return this.column41; } set { this.column41 = value; } }
        public int Column42 { get { return this.column42; } set { this.column42 = value; } }
        public int Column43 { get { return this.column43; } set { this.column43 = value; } }
        public int Column44 { get { return this.column44; } set { this.column44 = value; } }
        public int Column45 { get { return this.column45; } set { this.column45 = value; } }
        public int Column46 { get { return this.column46; } set { this.column46 = value; } }
        public int Column47 { get { return this.column47; } set { this.column47 = value; } }
        public int Column48 { get { return this.column48; } set { this.column48 = value; } }
        public int Column49 { get { return this.column49; } set { this.column49 = value; } }
        public int Column50 { get { return this.column50; } set { this.column50 = value; } }
        public int Column51 { get { return this.column51; } set { this.column51 = value; } }
        public int Column52 { get { return this.column52; } set { this.column52 = value; } }
        public int Column53 { get { return this.column53; } set { this.column53 = value; } }
        public int Column54 { get { return this.column54; } set { this.column54 = value; } }
        public int Column55 { get { return this.column55; } set { this.column55 = value; } }
        public int Column56 { get { return this.column56; } set { this.column56 = value; } }
        public int Column57 { get { return this.column57; } set { this.column57 = value; } }
        public int Column58 { get { return this.column58; } set { this.column58 = value; } }
        public int Column59 { get { return this.column59; } set { this.column59 = value; } }
        public int Column60 { get { return this.column60; } set { this.column60 = value; } }
        public int Column61 { get { return this.column61; } set { this.column61 = value; } }
        public int Column62 { get { return this.column62; } set { this.column62 = value; } }
        public int Column63 { get { return this.column63; } set { this.column63 = value; } }
        public int Column64 { get { return this.column64; } set { this.column64 = value; } }
        public int Column65 { get { return this.column65; } set { this.column65 = value; } }
        public int Column66 { get { return this.column66; } set { this.column66 = value; } }
        public int Column67 { get { return this.column67; } set { this.column67 = value; } }
        public int Column68 { get { return this.column68; } set { this.column68 = value; } }
        public int Column69 { get { return this.column69; } set { this.column69 = value; } }
        public int Column70 { get { return this.column70; } set { this.column70 = value; } }
        public int Column71 { get { return this.column71; } set { this.column71 = value; } }
        public int Column72 { get { return this.column72; } set { this.column72 = value; } }
        public int Column73 { get { return this.column73; } set { this.column73 = value; } }
        public int Column74 { get { return this.column74; } set { this.column74 = value; } }
        public int Column75 { get { return this.column75; } set { this.column75 = value; } }
        public int Column76 { get { return this.column76; } set { this.column76 = value; } }
        public int Column77 { get { return this.column77; } set { this.column77 = value; } }
        public int Column78 { get { return this.column78; } set { this.column78 = value; } }
        public int Column79 { get { return this.column79; } set { this.column79 = value; } }
        public int Column80 { get { return this.column80; } set { this.column80 = value; } }
        public int Column81 { get { return this.column81; } set { this.column81 = value; } }
        public int Column82 { get { return this.column82; } set { this.column82 = value; } }
        public int Column83 { get { return this.column83; } set { this.column83 = value; } }
        public int Column84 { get { return this.column84; } set { this.column84 = value; } }
        public int Column85 { get { return this.column85; } set { this.column85 = value; } }
        public int Column86 { get { return this.column86; } set { this.column86 = value; } }
        public int Column87 { get { return this.column87; } set { this.column87 = value; } }
        public int Column88 { get { return this.column88; } set { this.column88 = value; } }
        public int Column89 { get { return this.column89; } set { this.column89 = value; } }
        public int Column90 { get { return this.column90; } set { this.column90 = value; } }
        public int Column91 { get { return this.column91; } set { this.column91 = value; } }
        public int Column92 { get { return this.column92; } set { this.column92 = value; } }
        public int Column93 { get { return this.column93; } set { this.column93 = value; } }
        public int Column94 { get { return this.column94; } set { this.column94 = value; } }
        public int Column95 { get { return this.column95; } set { this.column95 = value; } }
        public int Column96 { get { return this.column96; } set { this.column96 = value; } }
        public int Column97 { get { return this.column97; } set { this.column97 = value; } }
        public int Column98 { get { return this.column98; } set { this.column98 = value; } }
        public int Column99 { get { return this.column99; } set { this.column99 = value; } }
        public int Column100 { get { return this.column100; } set { this.column100 = value; } }
    }
}
