C#/VB.NET: Delete Blank Rows and Columns in Excel

When working with an Excel document, some blank rows or columns may appear due to the modification of data. Although having blank rows or columns in a worksheet isn't necessarily a bad thing, in most cases they are still undesirable. Furthermore, such blank rows or columns may cause errors if formulas are applied. To delete these blank rows or columns, you can simply select and delete them, but if there are a lot of blank rows and columns, deleting them manually is a very time-consuming task. In this article, you will learn how to programmatically delete blank rows and columns in an Excel worksheet using Spire.XLS for .NET.

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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Delete Blank Rows and Columns in Excel

The detailed steps are as follows.

  • Instantiate a Workbook object.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Loop through all used rows in the specified worksheet and determine whether the row is blank using XlsRange.IsBlank property.
  • Delete the blank rows using Worksheet.DeleteRow() method.
  • Loop through all used columns in the specified worksheet and determine whether the column is blank using XlsRange.IsBlank property.
  • Delete the blank columns using Worksheet.DeleteColumn() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using System.Linq;
using Spire.Xls;

namespace DeleteBlankRowsAndColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Workbook object
            Workbook workbook = new Workbook();

            //Load a sample Excel file
            workbook.LoadFromFile("sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Delete blanks rows 
            for (int i = sheet.Rows.Count() - 1; i >= 0; i--)
            {
                if (sheet.Rows[i].IsBlank)
                {
                    sheet.DeleteRow(i + 1); //Index parameter in DeleteRow method starts from 1
                }
            }

            //Delete blank columns
            for (int j = sheet.Columns.Count() - 1; j >= 0; j--)
            {
                if (sheet.Columns[j].IsBlank)
                {
                    sheet.DeleteColumn(j + 1); //Index parameter in DeleteColumn method starts from 1
                }
            }

            //Save the file
            workbook.SaveToFile("DeleteBlankRowsAndColumns.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Delete Blank Rows and Columns in Excel

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.