Ejemplo n.º 1
0
 private void ExportHelper(IWorksheet Sheet, GridModel gridModel, GridStyleInfo Style, RowColumnIndex RowColIndex)
 {
     if (Sheet != null)
     {
         int ivalue; double dvalue; bool bvalue;
         try
         {
             IRange range = Sheet.Range[RowColIndex.RowIndex, RowColIndex.ColumnIndex];
             if (!range.HasFormula || Style.Text.Length == 0 || (Style.Text.Length > 0 && Style.Text[0] != '='))
             {
                 //To new Save Formula
                 if (Style.CellValue != null && Style.CellValue.ToString() != string.Empty && Style.CellValue.ToString()[0] == '=')
                 {
                     if (Style.CellValue.ToString().Contains('!'))
                     {
                         range.Formula = ExcelLikeFormula(gridModel, Style.CellValue.ToString());
                     }
                     else
                     {
                         range.Formula = Style.CellValue.ToString().ToUpper();
                     }
                     if (int.TryParse(Style.FormattedText, out ivalue))
                     {
                         Style.HorizontalAlignment           = System.Windows.HorizontalAlignment.Right;
                         range.CellStyle.HorizontalAlignment = ExcelHAlign.HAlignRight;
                     }
                     else if (double.TryParse(Style.FormattedText, out dvalue))
                     {
                         Style.HorizontalAlignment           = System.Windows.HorizontalAlignment.Right;
                         range.CellStyle.HorizontalAlignment = ExcelHAlign.HAlignRight;
                     }
                 }
                 else
                 {
                     string cellvalue = string.Empty;
                     if (Style.CellValue != null)
                     {
                         cellvalue = Style.CellValue.ToString();
                     }
                     if (Style.CellType == "FormulaCell" && !string.IsNullOrEmpty(Style.Text) && Style.Text[0] == '=')
                     {
                         cellvalue = Style.FormattedText;
                     }
                     //To Save RichText
                     else if (Style.CellType == "RichText")
                     {
                         RichTextBoxHelper.SetRichTextValue(range, Style.CellValue as FlowDocument, Style.FormattedText);
                         return;
                     }
                     //To Save Number
                     if (int.TryParse(cellvalue, out ivalue))
                     {
                         range.Number = ivalue;
                     }
                     else if (double.TryParse(cellvalue, out dvalue))
                     {
                         range.Number = dvalue;
                     }
                     //To Save Boolean value
                     else if (bool.TryParse(cellvalue, out bvalue))
                     {
                         range.Boolean = bvalue;
                         if (bvalue)
                         {
                             Style.CellValue = "TRUE";
                         }
                         else
                         {
                             Style.CellValue = "FALSE";
                         }
                     }
                     //To Save other value
                     else
                     {
                         range.Value = cellvalue;
                     }
                 }
             }
             //To Save Formula
             else
             {
                 if (Style.FormulaTag != null && Style.CellValue != null && Style.CellValue.ToString() != string.Empty && Style.CellValue.ToString()[0] == '=')
                 {
                     if (!range.HasFormula)
                     {
                         if (Style.CellValue.ToString().Contains('!'))
                         {
                             range.Formula = ExcelLikeFormula(gridModel, Style.CellValue.ToString());
                         }
                         else
                         {
                             range.Formula = Style.CellValue.ToString().ToUpper();
                         }
                     }
                     else
                     {
                         range.Formula = ExcelLikeFormula(gridModel, Style.CellValue.ToString());
                     }
                 }
                 else if (!range.HasFormula)
                 {
                     range.Text = Style.FormattedText;
                 }
             }
         }
         catch (Exception)
         {
         }
     }
 }