/// <summary> /// Gets or sets cell /// </summary> /// <param name="rowIndex">Vertical position of cell</param> /// <param name="columnIndex">Horizontal position of cell</param> /// <returns>Returns Cell at given position or creates</returns> public Cell this[Int32 rowIndex, Int32 columnIndex] { get { // Check if given row and column indexes are not in range // of current collection or cell on that position is null if (this.Cells.Count <= rowIndex || this.Cells[rowIndex].Count <= columnIndex || this.Cells[rowIndex][columnIndex] == null) { // Create new cell on that position this[rowIndex, columnIndex] = new Cell(rowIndex, columnIndex); } // Return cell on given position return this.Cells[rowIndex][columnIndex]; } set { // Create needed space for rows if needed while (this.Cells.Count <= rowIndex) Cells.Add(new List<Cell>()); // Create needed space for columns if needed while (this.Cells[rowIndex].Count <= columnIndex) Cells[rowIndex].Add(null); // Create row definitions if needed while (Sheet.Grid.RowDefinitions.Count <= rowIndex) { RowDefinition rowDefinition = new RowDefinition() { Height = new GridLength(Sheet.DefaultCellHeight) }; Sheet.Grid.RowDefinitions.Add(rowDefinition); } // Add column definitions if needed while (Sheet.Grid.ColumnDefinitions.Count <= columnIndex) { ColumnDefinition columnDefinition = new ColumnDefinition() { Width = new GridLength(Sheet.DefaultCellWidth) }; Sheet.Grid.ColumnDefinitions.Add(columnDefinition); } // Add cell to collection and cells grid Cells[rowIndex][columnIndex] = value; Sheet.Grid.Children.Add(Cells[rowIndex][columnIndex]); } }
/// <summary> /// Imports all cells from opened document /// </summary> private void ImportWorksheet() { // Gets cell range from loaded sheet CellRange usedCells = worksheet.GetUsedCellRange(true); // Is there are any used cells if (usedCells != null && usedCells.Count() > 0) { // Go throught all of them for (int rowIndex = 0; rowIndex < usedCells.Height; rowIndex++) { for (int columnIndex = 0; columnIndex < usedCells.Width; columnIndex++) { // Create new cell for each of loaded used cells, // set cells position according to current index and // import call data Cell cell = new Cell(usedCells.FirstRowIndex + rowIndex, usedCells.FirstColumnIndex + columnIndex); cell.ImportData(usedCells[rowIndex, columnIndex]); //SheetGrid.Children.Add(cell); // TODO import grid definitions // Add cell to list cells.Add(cell); } } } }
/// <summary> /// Adds given cell to list /// </summary> /// <param name="cell">Cell to add to list</param> public void Add(Cell cell) { this[cell.PositionY, cell.PositionX] = cell; }