Delete Excel rows and columns in C#, VB.NET
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 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
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 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
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 rows and columns in C#, VB.NET.
You can refer to our WinForms Excel feature tour 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 WinForms Excel 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!