Exemplo n.º 1
0
        /// <summary>
        /// Checks if the given cell is merged and if so returns the last column or row from this merge.
        /// When the cell is not merged it just returns the cell
        /// </summary>
        /// <param name="range">The cell</param>
        /// <param name="searchOrder"><see cref="MergedCellSearchOrder"/></param>
        /// <returns></returns>
        private static int CheckForMergedCell(ExcelInterop.Range range, MergedCellSearchOrder searchOrder)
        {
            if (range == null)
                return 0;

            var result = 0;
            var mergeArea = range.MergeArea;

            switch (searchOrder)
            {
                case MergedCellSearchOrder.FirstRow:
                    result = mergeArea.Row;
                    break;

                case MergedCellSearchOrder.FirstColumn:
                    result = mergeArea.Column;
                    break;

                case MergedCellSearchOrder.LastRow:
                {
                    result = range.Row;
                    var entireRow = range.EntireRow;

                    for (var i = 1; i < range.Column; i++)
                    {
                        var cell = (ExcelInterop.Range) entireRow.Cells[i];
                        var cellMergeArea = cell.MergeArea;
                        var cellMergeAreaRows = cellMergeArea.Rows;
                        var count = cellMergeAreaRows.Count;

                        Marshal.ReleaseComObject(cellMergeAreaRows);
                        Marshal.ReleaseComObject(cellMergeArea);
                        Marshal.ReleaseComObject(cell);

                        var tempResult = result;

                        if (count > 1 && range.Row + count > tempResult)
                            tempResult = result + count;

                        result = tempResult;
                    }

                    Marshal.ReleaseComObject(entireRow);

                    break;
                }

                case MergedCellSearchOrder.LastColumn:
                {
                    result = range.Column;
                    var columns = mergeArea.Columns;
                    var count = columns.Count;

                    if (count > 1)
                        result += count;

                    Marshal.ReleaseComObject(columns);

                    break;
                }
            }

            if (mergeArea != null)
                Marshal.ReleaseComObject(mergeArea);

            return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if the given cell is merged and if so returns the last column or row from this merge.
        /// When the cell is not merged it just returns the cell
        /// </summary>
        /// <param name="range">The cell</param>
        /// <param name="searchOrder"><see cref="MergedCellSearchOrder"/></param>
        /// <returns></returns>
        private static int CheckForMergedCell(ExcelInterop.Range range, MergedCellSearchOrder searchOrder)
        {
            if (range == null)
            {
                return(0);
            }

            var result    = 0;
            var mergeArea = range.MergeArea;

            switch (searchOrder)
            {
            case MergedCellSearchOrder.FirstRow:
                result = mergeArea.Row;
                break;

            case MergedCellSearchOrder.FirstColumn:
                result = mergeArea.Column;
                break;

            case MergedCellSearchOrder.LastRow:
            {
                result = range.Row;
                var entireRow = range.EntireRow;

                for (var i = 1; i < range.Column; i++)
                {
                    var cell              = (ExcelInterop.Range)entireRow.Cells[i];
                    var cellMergeArea     = cell.MergeArea;
                    var cellMergeAreaRows = cellMergeArea.Rows;
                    var count             = cellMergeAreaRows.Count;

                    Marshal.ReleaseComObject(cellMergeAreaRows);
                    Marshal.ReleaseComObject(cellMergeArea);
                    Marshal.ReleaseComObject(cell);

                    var tempResult = result;

                    if (count > 1 && range.Row + count > tempResult)
                    {
                        tempResult = result + count;
                    }

                    result = tempResult;
                }

                Marshal.ReleaseComObject(entireRow);

                break;
            }

            case MergedCellSearchOrder.LastColumn:
            {
                result = range.Column;
                var columns = mergeArea.Columns;
                var count   = columns.Count;

                if (count > 1)
                {
                    result += count;
                }

                Marshal.ReleaseComObject(columns);

                break;
            }
            }

            if (mergeArea != null)
            {
                Marshal.ReleaseComObject(mergeArea);
            }

            return(result);
        }