private static CellValue EncodeCell(Cell cell, SharedResource sharedResource)
 {
     object value = cell.Value;
     if (value is int)
     {
         RK rk = new RK();
         rk.Value = (uint)((int)value << 2 | 2);
         return rk;
     }
     else if (value is decimal)
     {
         RK rk = new RK();
         rk.Value = (uint)((decimal)value * 100) << 2 | 3; // integer and mul
         return rk;
     }
     else if (value is double)
     {
         //RK rk = new RK();
         //Int64 data = BitConverter.DoubleToInt64Bits((double)value);
         //rk.Value = (uint)(data >> 32) & 0xFFFFFFFC;
         //return rk;
         NUMBER number = new NUMBER();
         number.Value = (double)value;
         return number;
     }
     else if (value is string)
     {
         LABELSST label = new LABELSST();
         label.SSTIndex = sharedResource.GetSSTIndex((string)value);
         return label;
     }
     else if (value is DateTime)
     {
         NUMBER number = new NUMBER();
         number.Value = sharedResource.EncodeDateTime((DateTime)value);
         return number;
     }
     else if (value is bool)
     {
         BOOLERR boolerr = new BOOLERR();
         boolerr.ValueType = 0;
         boolerr.Value = Convert.ToByte((bool)value);
         return boolerr;
     }
     else if (value is ErrorCode)
     {
         BOOLERR boolerr = new BOOLERR();
         boolerr.ValueType = 1;
         boolerr.Value = ((ErrorCode)value).Code;
         return boolerr;
     }
     else
     {
         throw new Exception("Invalid cell value.");
     }
 }
        //====================================================================================================================================
        public static void YtgYtdExportToExcel(DataGrid theGrid)
        {
            // open file dialog to select an export file.   
            SaveFileDialog sDialog = new SaveFileDialog();
            sDialog.Filter = "Excel Files(*.xls)|*.xls";

            if (sDialog.ShowDialog() == true)
            {

                // create an instance of excel workbook
                Workbook workbook = new Workbook();
                // create a worksheet object
                Worksheet worksheet = new Worksheet("Data");
                
                Int16 ColumnCount = 0;
                Int16 RowCount = 0;

                //Writing Column Names 
                foreach (DataGridColumn dgcol in theGrid.Columns)
                {
                    string header ="";
                    if (ColumnCount == 0)
                    {
                        Setter setter = (Setter)dgcol.HeaderStyle.Setters[0];
                        ControlTemplate tem = (ControlTemplate)setter.Value;
                        Grid contentGrid = (Grid)GetTemplateChildByName(theGrid, "ContentGrid");
                        StackPanel stack = (StackPanel)contentGrid.Children[0];
                        header = (stack.Children[0] as TextBlock).Text + "\n" + (stack.Children[1] as TextBlock).Text+ "\n" + (stack.Children[2] as TextBlock).Text;
                    }

                    if(dgcol.Header == null)
                        dgcol.Header = header;

                    worksheet.Cells[0, ColumnCount] = new Cell(dgcol.Header.ToString());
                    ColumnCount++;
                }

                //Extracting values from grid and writing to excell sheet
                //
                foreach (object data in theGrid.ItemsSource)
                {
                    ColumnCount = 0;
                    RowCount++;
                    foreach (DataGridColumn col in theGrid.Columns)
                    {

                        string strValue = "";
                        Binding objBinding = null;
                        if (col is DataGridBoundColumn)
                            objBinding = (col as DataGridBoundColumn).Binding;
                        if (col is DataGridTemplateColumn)
                        {
                            //This is a template column... let us see the underlying dependency object
                            DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
                            FrameworkElement oFE = (FrameworkElement)objDO;
                            FieldInfo oFI = oFE.GetType().GetField("TextProperty");
                            if (oFI != null)
                            {
                                if (oFI.GetValue(null) != null)
                                {
                                    if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null)
                                        objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding;
                                }
                            }
                        }
                        if (objBinding != null)
                        {
                            if (objBinding.Path.Path != "")
                            {
                                PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
                                if (pi != null) strValue = Convert.ToString(pi.GetValue(data, null));
                            }
                            if (objBinding.Converter != null)
                            {
                                if (strValue != "")
                                    strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
                                //else
                                //    strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
                            }
                        }
                        // writing extracted value in excell cell
                        Cell cell = new Cell(strValue);
                        cell.Style = new CellStyle {  BackColor = Colors.Orange, RichTextFormat = new Lite.ExcelLibrary.BinaryFileFormat.RichTextFormat(2)};
                        worksheet.Cells[RowCount, ColumnCount] = cell;

                        ColumnCount++;   
                    }
                }
                //add worksheet to workbook
                workbook.Worksheets.Add(worksheet);
                // get the selected file's stream
                Stream sFile = sDialog.OpenFile();
                workbook.Save(sFile);
            }
        }