When the data in specific rows or columns are no longer needed, you can delete those rows or columns from your worksheet. In this article, you will learn how to delete rows and columns from Excel in C# and VB.NET using Spire.XLS for .NET library.
Install Spire.XLS for.NET
To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Delete a Specific Row and Column from Excel in C# and VB.NET
The following are the steps to delete a specific row and column from an Excel worksheet:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Delete the desired row from the worksheet by its index (1-based) using Worksheet.DeleteRow(rowIndex) method.
- Delete the desired column from the worksheet by its index (1-based) using Worksheet.DeleteColumn(columnIndex) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace DeleteRowAndColumn { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Delete the 9th row sheet.DeleteRow(9); //Delete the 3rd column sheet.DeleteColumn(3); //Save the result file workbook.SaveToFile("DeleteRowAndColumn.xlsx", ExcelVersion.Version2016); } } }
Delete Multiple Rows and Columns from Excel in C# and VB.NET
The following are the steps to delete multiple rows and columns from an Excel worksheet:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Delete the desired rows from the worksheet using Worksheet.DeleteRow(startRowIndex, rowCount) method.
- Delete the desired columns from the worksheet using Worksheet.DeleteColumn(startColumnIndex, columnCount) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace DeleteMultipleRowsAndColumns { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile(@"Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Delete 3 rows from the worksheet starting from the 7th row sheet.DeleteRow(7, 3); //Delete 3 columns from the worksheet starting from the 3rd column sheet.DeleteColumn(3, 3); //Save the result file workbook.SaveToFile("DeleteMultipleRowsAndColumns.xlsx", ExcelVersion.Version2016); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.