Documents for Excel .NET Edition | GCDocuments
Features / Worksheet / Range Operations / Insert and Delete Rows and Columns
In This Topic
    Insert and Delete Rows and Columns
    In This Topic

    GcExcel provides you with the ability to insert or delete rows and columns in a worksheet.

    Insert rows and columns

    GcExcel allow you to add rows or columns in a worksheet by calling Insert method of IRange.

    When rows are added, the existing rows in the worksheet are shifted in downward direction whereas when columns are added, the existing columns in the worksheet are shifted to the right.

    You can also use the EntireRow property to insert rows in a worksheet which includes all the columns. While inserting rows using the EntireRow property, there is no need to provide the shift direction in the function parameters. If you provide the same, it will be ignored.

    Refer to the following example code to insert rows in a worksheet.

    C#
    Copy Code
    //Insert rows
    worksheet.Range["A3:A5"].EntireRow.Insert();
    // OR
    worksheet.Range["3:5"].Insert(InsertShiftDirection.Down);

    You can also use the EntireColumn property to insert columns in the worksheet which includes all rows. While inserting columns using the EntireColumn property, there is no need to provide the shift direction in the function parameters. If you provide the same, it will be ignored.

    Refer to the following example code to insert columns in a worksheet.

    C#
    Copy Code
    //Insert column
    worksheet.Range["A3:B5"].EntireColumn.Insert();
    // OR
    worksheet.Range["B:C"].Insert(InsertShiftDirection.Down);

    Delete row and column

    GcExcel allows you to delete rows or columns in the worksheet by calling Delete method of IRange.

    When rows are deleted, the existing rows in the worksheet are shifted in upwards direction, whereas when columns are deleted, the existing columns in the worksheet are shifted to the left.

    Refer to the following example code to delete rows from the worksheet.

    C#
    Copy Code
    //Delete rows
    worksheet.Range["A3:A5"].EntireRow.Delete();
    // OR
    worksheet.Range["3:5"].Delete();

    Refer to the following example code to delete columns from the worksheet.

    C#
    Copy Code
    //Delete Columns
    worksheet.Range["A3:A5"].EntireColumn.Delete();
    // OR
    worksheet.Range["A:A"].Delete(DeleteShiftDirection.Left);

    See Also