void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) { var dataContext = AssociatedObject.DataContext as ExcelImportViewModel; if (dataContext != null) { SetExcelLikeUI(e); if (e.Cell.RowIndex <= 0 || e.Cell.ColumnIndex <= 0) { return; } GridModel gridModel = sender as GridModel; if (!e.Style.IsChanged && e.Style.IsEmpty) { int index = dataContext.ActiveTabIndex; IWorksheet sheet = dataContext.Workbook.Worksheets[index]; if (sheet != null) { IRange range = sheet.Range; if (e.Cell.RowIndex >= range.Row && e.Cell.ColumnIndex >= range.Column) { IRange rangeToConvert = sheet.Range[e.Cell.RowIndex, e.Cell.ColumnIndex]; GridModelImportExtensions.ConvertExcelRangeToVirtualGrid(e.Style, sheet, rangeToConvert, null); if (gridModel != null && e.Style.FormulaTag == null) { string s = e.Style.Text; if (s.Length > 0 && s[0] == '=') { s = s.Substring(1); try { gridModel.FormulaEngine.FormulaContextCell = GridRangeInfo.GetAlphaLabel(e.Cell.ColumnIndex) + e.Cell.RowIndex.ToString(); e.Style.FormulaTag = new GridFormulaTag(gridModel.FormulaEngine.Parse(s), null, e.Cell.RowIndex, e.Cell.ColumnIndex); string cellvalue = rangeToConvert.DisplayText; e.Style.FormulaTag.Text = cellvalue; } catch (Exception ex) { e.Style.FormulaTag = new GridFormulaTag(ex.Message, ex.Message, e.Cell.RowIndex, e.Cell.ColumnIndex); } } } gridModel.Data[e.Cell.RowIndex, e.Cell.ColumnIndex] = e.Style.Store; } } } else { // To save the modified value. int index = dataContext.ActiveTabIndex; IWorksheet changedSheet = dataContext.Workbook.Worksheets[index]; if (changedSheet != null) { IRange range = changedSheet.Range[e.Cell.RowIndex, e.Cell.ColumnIndex]; SetAlignment(range, e.Style); } } } }
public void LoadWorkbook(IWorkbook Workbook) { tControl.Items.Clear(); gridModelCollection = GridModelImportExtensions.ImportFromExcelToVirtualGrid(Workbook); for (int i = 0; i < Workbook.Worksheets.Count; i++) { GridControl grid = new GridControl(); grid.Model = GridModelCollection[i]; grid.Model.ColumnWidths[0] = 35d; grid.Model.Options.WrapCell = false; grid.Model.Options.EnableFloatingCell = true; grid.Model.Options.ExcelLikeFreezePane = true; grid.Model.Options.ExcelLikeCurrentCell = true; grid.Model.Options.AllowExcelLikeResizing = true; grid.Model.Options.ExcelLikeSelectionFrame = true; grid.Model.CellModels.Add("SparkLineCell", new SparklineCellModel()); grid.Model.GraphicModel.CellModels.Add("Chart", new GraphicChartCellModel()); grid.Model.TableStyle.TextWrapping = TextWrapping.NoWrap; grid.Model.TableStyle.FloatCellMode = GridFloatCellsMode.OnDemandCalculation; grid.Model.Options.ActivateCurrentCellBehavior = GridCellActivateAction.DblClickOnCell; grid.Model.Options.CopyPasteOption = CopyPaste.CopyText | CopyPaste.CutText | CopyPaste.PasteText; GridCommentService.SetShowComment(grid, true); BehaviorCollection GridBehaviors = Interaction.GetBehaviors(grid); GridBehaviors.Add(new GridCellImportingBehavior()); GridBehaviors.Add(new GridCellExportingBehavior()); GridBehaviors.Add(new CurrentCellEditingBehavior()); GridBehaviors.Add(new CellRequestNavigateBehavior()); GridBehaviors.Add(new CurrentCellSyncBehavior()); TabItemExt tab = new TabItemExt(); tab.Header = Workbook.Worksheets[i].Name; ScrollViewer sv = new ScrollViewer(); sv.CanContentScroll = true; sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; sv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; sv.Content = grid; tab.Content = sv; if (Workbook.Worksheets[i].Visibility != WorksheetVisibility.Visible) { tab.Visibility = System.Windows.Visibility.Collapsed; } tControl.Items.Add(tab); } tControl.SelectedIndex = 0; }