Thursday, 07 March 2013 06:24

Delete Excel Rows and Columns in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

This section aims at providing developers a solution on how to delete Excel rows and columns in C#, VB.NET via this .NET Excel component Spire.XLS for .NET.

Spire.XLS for .NET enables users to delete any Excel rows and columns through two methods: Spire.Xls.Worksheet.DeleteRow(int index) and Spire.Xls.Worksheet.DeleteColumn(int index). Please allow me to give a short description about Spire.XLS for .NET if you are a new visitor here.

Spire.XLS for .NET is a library which provides developers an incredible wealth of features to handle Excel documents without using MS Excel or any third party libraries. Here you can download Spire.XLS for .NET. Then, install it on your system. Since you will use Spire.XLS for .NET, you have to add Spire.XLS.dll as reference from the Bin folder after you start a project in Visual Studio. The default path is “..\Spire.XLS\Bin\NET4.0\ Spire.XLS.dll”. Below is the whole code in my project:

[C#]
//initialize a new Workbook object 
Workbook workbook = new Workbook();
//open an excel file
workbook.LoadFromFile(@"..\excel.xlsx");
//get the first worksheet 
Worksheet sheet = workbook.Worksheets[0];
//delete the twelfth row of the first sheet
sheet.DeleteRow(12);
//delete the second column of the first sheet
sheet.DeleteColumn(2); 
//save the excel file
workbook.SaveToFile("sample.xlsx", ExcelVersion.Version2010);
//launch the excel file
System.Diagnostics.Process.Start(workbook.FileName);

 

[VB.NET]
'initialize a new Workbook object 
Dim workbook As New Workbook()
'open an excel file
workbook.LoadFromFile("..\excel.xlsx")
'get the first worksheet 
Dim sheet As Worksheet = workbook.Worksheets(0)
'delete the twelfth row of the first sheet
sheet.DeleteRow(12)
'delete the second column of the first sheet
sheet.DeleteColumn(2)
'save the excel file
workbook.SaveToFile("sample.xlsx", ExcelVersion.Version2010)
'launch the excel file
System.Diagnostics.Process.Start(workbook.FileName)

Output File:

After executing above code, data of row 12 and column B in the original Excel file is deleted, you can make a comparison of both original Excel file and the target Excel file as below:

Delete Excel Rows and Columns

original Excel file.jpg

Delete Excel Rows and Columns

delete rows and columns.jpg

In this section, I have introduced a solution on how to delete Excel rows and columns in C#, VB.NET. From above code, you may find that it is very easy for Spire.XLS for .NET users to finish the delete row and column task. If you have any questions, comments and suggestions, you can put them on E-iceblue Forum. We guarantee to give prompt reply.

Spire.XLS for .NET is an Excel library which allows developers to create, read, edit and manipulate Excel files in C#, VB.NET.

More about this .NET Excel Library

Additional Info

  • tutorial_title: Delete Excel Rows and Columns
Last modified on Wednesday, 08 January 2014 02:22