/// <summary> /// Inserts one or more rows before the specified position in the table. /// </summary> /// <param name="position">The position in the table where the row will be inserted. Default is in the end of the table. 0 will insert the row at the top. Any value larger than the number of rows in the table will insert a row at the bottom of the table.</param> /// <param name="rows">Number of rows to insert.</param> /// <returns>The inserted range</returns> public ExcelRangeBase InsertRow(int position, int rows = 1) { if (position < 0) { throw new ArgumentException("position", "position can't be negative"); } if (rows < 0) { throw new ArgumentException("position", "rows can't be negative"); } var subtact = ShowTotal ? 2 : 1; if (position >= ExcelPackage.MaxRows || position > _address._fromRow + position + rows - subtact) { position = _address.Rows - subtact; } if (_address._fromRow + position + rows > ExcelPackage.MaxRows) { throw new InvalidOperationException("Insert will exceed the maximum number of rows in the worksheet"); } position++; var address = ExcelCellBase.GetAddress(_address._fromRow + position, _address._fromCol, _address._fromRow + position + rows - 1, _address._toCol); var range = new ExcelRangeBase(WorkSheet, address); WorksheetRangeInsertHelper.Insert(range, eShiftTypeInsert.Down, false); if (range._toRow > _address._toRow) { Address = _address.AddRow(_address._toRow, rows); } return(range); }
/// <summary> /// Inserts one or more columns before the specified position in the table. /// </summary> /// <param name="position">The position in the table where the column will be inserted. 0 will insert the column at the leftmost. Any value larger than the number of rows in the table will insert a row at the bottom of the table.</param> /// <param name="columns">Number of rows to insert.</param> /// <returns>The inserted range</returns> internal ExcelRangeBase InsertColumn(int position, int columns) { if (position < 0) { throw new ArgumentException("position", "position can't be negative"); } if (columns < 0) { throw new ArgumentException("columns", "columns can't be negative"); } if (position >= ExcelPackage.MaxColumns || position > _address._fromCol + position + columns - 1) { position = _address.Columns; } if (_address._fromCol + position + columns - 1 > ExcelPackage.MaxColumns) { throw new InvalidOperationException("Insert will exceed the maximum number of columns in the worksheet"); } var address = ExcelCellBase.GetAddress(_address._fromRow, _address._fromCol + position, _address._toRow, _address._fromCol + position + columns - 1); var range = new ExcelRangeBase(WorkSheet, address); WorksheetRangeInsertHelper.Insert(range, eShiftTypeInsert.Right, false); if (position == 0) { Address = new ExcelAddressBase(_address._fromRow, _address._fromCol - columns, _address._toRow, _address._toCol); } else if (range._toCol > _address._toCol) { Address = _address.AddColumn(_address._toCol, columns); } return(range); }