예제 #1
0
        /// <summary>
        /// Adds cells to the sheet without checking for its existance.
        /// The method is used by Importing Bulk data methods exposed by OpenXmlPackaging
        /// </summary>
        /// <param name="row">Cell Row</param>
        /// <param name="column">Cell Column</param>
        /// <param name="value">Value of the cell</param>
        internal void AddCell(int row, int column, string value)
        {
            // Get the current row
            var cellRow = AllCells.ContainsKey(row) ? AllCells[row] : null;

            // Add one if the row does not exist
            if (cellRow == null)
            {
                cellRow = new List <Cell>();
                AllCells.Add(row, cellRow);
            }

            // Create a cell
            var cell = new Cell(row, column)
            {
                Stylesheet = _styleSheet, Value = value
            };

            // Assign the Row property of the cell
            if (cellRow.Count == 0)
            {
                cell.Row = new Row(row);
            }
            else
            {
                cell.Row = cellRow[0].Row;
            }

            // Add the cell to the row
            cellRow.Add(cell);
        }
예제 #2
0
        /// <summary>
        /// Returns a cell at a given row, column.
        /// It creates a new cell if it does not exist.
        /// It will return the cell if it already exists
        /// </summary>
        /// <param name="rowNumber">Row Number</param>
        /// <param name="columnIndex">Column Index</param>
        /// <returns></returns>
        private Cell GetCell(int rowNumber, int columnIndex)
        {
            string reference = Utilities.BuildReference(rowNumber, columnIndex);
            var    row       = AllCells.FirstOrDefault(c => c.Key == rowNumber).Value;

            // Check if the cell already exists
            var cell = row == null ? null : row.FirstOrDefault(c => c.ColumnNumber == columnIndex);

            // Cell does not exist. Create one.
            if (cell == null)
            {
                cell = new Cell(reference)
                {
                    Stylesheet = _styleSheet
                };

                var currentRow = Rows.FirstOrDefault(r => r.Index == rowNumber);

                // If the row already exists, get the row. Else, create a new row
                List <Cell> thisRow = null;
                if (AllCells.ContainsKey(rowNumber))
                {
                    thisRow = AllCells[rowNumber];
                }
                else
                {
                    thisRow = new List <Cell>();
                    AllCells.Add(rowNumber, thisRow);
                }

                // Add cell to the current row.
                thisRow.Add(cell);

                // Add the current row to the list of rows if it does not exist in the list.
                if (currentRow == null)
                {
                    cell.Row = new Row(rowNumber);
                    Rows.Add(cell.Row);
                }
                else
                {
                    cell.Row = currentRow;
                }

                // Add the current column to the list of columns if it does not exist in the list.
                if (!Columns.ColumnsList.Any(c => c.Index == columnIndex))
                {
                    Columns.ColumnsList.Add(new Column {
                        Index = columnIndex
                    });
                }
            }

            return(cell);
        }