Articles in this section
Category / Section

How to delete Excel table rows and columns in C#, VB.NET?

5 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents.

This article explains how to delete table rows and columns without affecting the remaining tables or content in the Excel worksheet.

Logic to delete table row:

C#

private void DeleteTableRow(IListObject table, int rowNumber)
{
    IRange tableRange = table.Location;
    IWorksheet worksheet = table.Worksheet;
    int firstTableColumn = tableRange.Column;
    int lastTableColumn = tableRange.LastColumn;
    int firstTableRow = tableRange.Row;
    int lastTableRow = tableRange.LastRow;
 
    table.Worksheet[firstTableRow + rowNumber, firstTableColumn, firstTableRow + rowNumber, lastTableColumn].Clear(ExcelClearOptions.ClearContent);
    if (firstTableRow + rowNumber < tableRange.LastRow)
    {
        for (int i = firstTableRow + rowNumber + 1; i <= worksheet.Range.LastRow; i++)
        {
            worksheet.Range[i, firstTableColumn, i, lastTableColumn].MoveTo(worksheet.Range[i - 1, firstTableColumn, i - 1, lastTableColumn]);
        }
    }
    table.Location = table.Location[firstTableRow, firstTableColumn, lastTableRow - 1, lastTableColumn];
 
    for (int i = 0; i < worksheet.ListObjects.Count; i++)
    {
        IListObject tableSample = worksheet.ListObjects[i];
        int firstrow = tableSample.Location.Row;
        int firstcolumn = tableSample.Location.Column;
        if (table.Location.LastRow < firstrow && table.Location.LastColumn >= firstcolumn)
        {
            tableSample.Location = table.Location[firstrow - 1, firstcolumn, tableSample.Location.LastRow - 1, tableSample.Location.LastColumn];
        }
    }
}

 

Logic to delete table column:

C#

private void DeleteTableColumn(IListObject table, int columnNumber)
{
    IRange tableRange = table.Location;
    IWorksheet worksheet = table.Worksheet;
    int firstTableColumn = tableRange.Column;
    int lastTableColumn = tableRange.LastColumn;
    int firstTableRow = tableRange.Row;
    int lastTableRow = tableRange.LastRow;
 
    table.Worksheet[firstTableRow, firstTableColumn + columnNumber, lastTableRow, firstTableColumn + columnNumber].Clear(ExcelClearOptions.ClearContent);
    if (firstTableColumn + columnNumber < tableRange.LastColumn)
    {
        for (int i = firstTableColumn + columnNumber + 1; i <= worksheet.Range.LastColumn; i++)
        {
            worksheet.Range[firstTableRow, i, lastTableRow, i].MoveTo(worksheet.Range[firstTableRow, i - 1, lastTableRow, i - 1]);
        }
    }
    table.Location = table.Location[firstTableRow, firstTableColumn, lastTableRow, lastTableColumn - 1];
 
    for (int i = 0; i < worksheet.ListObjects.Count; i++)
    {
        IListObject tableSample = worksheet.ListObjects[i];
        int lastrow = tableSample.Location.LastRow;
        int firstcolumn = tableSample.Location.Column;
        if (table.Location.LastColumn < firstcolumn && table.Location.LastRow >= lastrow)
        {
            tableSample.Location = table.Location[tableSample.Location.Row, firstcolumn - 1, lastrow, tableSample.Location.LastColumn - 1];
        }
    }
}

 

Note:

The table column and table row to be parsed in above methods are zero-based.

 

The following complete code snippet explains how to delete table rows and columns.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Excel2016;
 
    IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];
 
    IListObject table = worksheet.ListObjects[0];
 
    Program prgm = new Program();
 
    //Delete table column
    table.Columns.RemoveAt(2);
    prgm.DeleteTableColumn(table, 2);
 
    //Delete table row
    prgm.DeleteTableRow(table, 4);
 
    workbook.SaveAs("Output.xlsx");
    System.Diagnostics.Process.Start("Output.xlsx");
}

 

Note:

To delete the table column first you need to remove the column and then delete. Whereas the table row can be deleted directly without removing.

A complete working sample can be downloaded from DeleteTableRowAndColumn.Zip.

The input Excel document looks like,

Excel file with table and content around the table

Excel file with table and content around the table

The output Excel document after deleting 3rd column and 5th row of Excel table looks like,

Excel file after deleting row and column

Excel file after deleting row and column

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions etc. with code examples.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Conclusion

I hope you enjoyed learning about how to delete excel table rows and columns in C#, VB.NET.

You can refer to our .NET Excel library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Excel library .NET Excel library example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or  feedbackportal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied