Example #1
0
        public static Range InsertCopyRange(Worksheet sheet, Range orign, int columns, int rows, int targetColumn,
                                            int targetRow, XlInsertShiftDirection direction,
                                            int lastColCount)
        {
            insertCopyRangeCallTimes++;
            // return GetRange(sheet, targetColumn, targetRow, columns, rows);
            int orgColumn = orign.Column;
            int orgRow    = orign.Row;

            Range target = GetRange(sheet, targetColumn, targetRow, columns, rows);

            if (direction == XlInsertShiftDirection.xlShiftToRight)
            {
                // insert blank
                // target.Insert (direction, Missing.Value) ;
                // Move target from origin to Right first

                Range movedRange     = GetRange(sheet, targetColumn, targetRow, lastColCount, rows);
                Range movedNextRange = GetRange(sheet, targetColumn + columns, targetRow, lastColCount, rows);
                movedRange.Move(movedNextRange, true, false);

                target = GetRange(sheet, targetColumn, targetRow, columns, rows);
                target.UnMerge();
            }
            orign.Copy(target, false, true);

            switch (direction)
            {
            case XlInsertShiftDirection.xlShiftDown:
                // copy row height ;
                for (int i = 0; i < rows; i++)
                {
                    Range sRow = GetEntireRow(sheet, i + orgRow);
                    Range tRow = GetEntireRow(sheet, i + targetRow);
                    try
                    {
                        tRow.RowHeight = sRow.RowHeight;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Set RowHeight Error: " + e);
                    }
                }
                break;

            case XlInsertShiftDirection.xlShiftToRight:
                // copy col width ;
                for (int i = 0; i < columns; i++)
                {
                    Range sCol = GetEntireCol(sheet, i + orgColumn);
                    Range tCol = GetEntireCol(sheet, i + targetColumn + 1);

                    try
                    {
                        tCol.ColumnWidth = sCol.ColumnWidth;
                    }catch (Exception e)
                    {
                        Console.WriteLine("Set ColumnWidth Error: " + e);
                    }
                }
                break;
            }


            return(target);
        }