We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Delete Excel rows and columns in C#, VB.NET

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: June 12, 2019

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can delete Excel rows and columns in C#, VB.NET.

The statement IWorksheet.DeleteRow(int index, int count) is meant for deleting the Excel rows. Similarly, the statement IWorksheet.DeleteColumn(int index, int count) is meant for deleting the Excel columns. DeleteRow method or DeleteColumn method contains two arguments which are used to delete Excel rows and columns as explained below.

  • Index - Starting position of the Excel rows or Excel columns to be deleted.
  • Count - Number of Excel rows or Excel columns to be deleted from the mentioned index.

‘DeleteRow’ and ‘DeleteColumn’ methods take input in one based index. Hence, to delete Excel rows starting from 3rd row, you must use worksheet.DeleteRow(3, 5); which deletes five rows (rows 3,4,5,6 and 7). Similarly, to delete Excel columns starting from 5th column, ‘’, you must use worksheet.DeleteColumn(5, 2); which deletes two columns (columns 5 and 6).

Steps to delete Excel rows and columns, programmatically:

Step 1: Create a new C# console application project.

Create C# Console Application

Create a new C# console application

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet Package

Install NuGet package

Step 3: Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Use the following code snippet to delete Excel rows and columns in C#, VB.NET.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
 
    //Add a workbook
    IWorkbook workbook = application.Workbooks.Open("DeleteRowsAndColumnsTemplate.xlsx", ExcelOpenType.Automatic);
 
    //The first worksheet object in the worksheets collection is accessed
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Delete Excel rows
    worksheet.DeleteRow(3, 5);                
 
    //Delete Excel columns
    worksheet.DeleteColumn(5, 2);
 
    //Save the workbook
    workbook.SaveAs("Output.xlsx");
}
 

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
 
    'Add a workbook
    Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/DeleteRowsandColumnsTemplate.xlsx", ExcelOpenType.Automatic)
 
    'The first worksheet object in the worksheets collection is accessed
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Delete Excel rows
    worksheet.DeleteRow(3, 5)
 
    'Delete Excel columns
    worksheet.DeleteColumn(5, 2)
 
    'Save the workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete Windows Forms working example of how to delete Excel rows and columns in C# and VB can be downloaded from Delete Excel Rows And Columns.zip.

By executing the program, you will get the output Excel document as shown below:

Output after deleting Excel rows and columns

Output Excel document

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

See Also:

Insert Excel rows and columns in C#, VB.NET

How do I group Rows and Columns

Why formulas are not updated when inserting rows?

How to delete Excel chart legend in C#, VB.NET?

How to UnFreeze the Rows and Columns in XlsIO

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon