News Category

C#/VB.NET Hide or Show Rows and Columns in Excel

2022-11-08 03:47:00 Written by  support iceblue
Rate this item
(0 votes)

When dealing with Excel files containing large amounts of data, you may sometimes need to hide certain rows and columns to conceal useless data so that you can focus on the information you need to analyze. In this article, you will learn how to hide or show rows and columns in Excel in C# and VB.NET 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

Hide Specific Rows and Columns in Excel in C# and VB.NET

The following steps demonstrate how to hide specific rows and columns in Excel in C# and VB.NET:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
  • Hide specific rows in the worksheet using Worksheet.HideRow(int rowIndex) method.
  • Hide Specific columns in the worksheet using Worksheet.HideColumn(int columnIndex) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace HideExcelRowsAndColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Input.xlsx");

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

            //Hide the 3rd and the 7th rows
            sheet.HideRow(3);
            sheet.HideRow(7);

            //Hide the 3rd and the 6th columns
            sheet.HideColumn(3);
            sheet.HideColumn(6);

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

C#/VB.NET Hide or Show Rows and Columns in Excel

Show Specific Hidden Rows and Columns in Excel in C# and VB.NET

The following steps demonstrate how to show specific hidden rows and columns in Excel in C# and VB.NET:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
  • Show specific hidden rows in the worksheet using Worksheet.ShowRow(int rowIndex) method.
  • Show specific hidden columns in the worksheet using Worksheet.ShowColumn(int columnIndex) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ShowExcelRowsAndColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("HideRowsAndColumns.xlsx");

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

            //Show the 3rd and the 7th rows
            sheet.ShowRow(3);
            sheet.ShowRow(7);

            //Show the 3rd and the 6th columns
            sheet.ShowColumn(3);
            sheet.ShowColumn(6);

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

C#/VB.NET Hide or Show Rows and Columns in Excel

Hide Multiple Rows and Columns at Once in Excel in C# and VB.NET

The following steps demonstrate how to hide multiple rows and columns at once in Excel in C# and VB.NET:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
  • Hide multiple rows in the worksheet using Worksheet.HideRows(int rowIndex, int rowCount) method.
  • Hide multiple columns in the worksheet using Worksheet.HideColumns(int columnIndex, int columnCount) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace HideMultipleExcelRowsAndColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Input.xlsx");

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

            //Hide 3, 4 and 5 rows
            sheet.HideRows(3, 3);

            //Hide 5, 6 and 7 columns
            sheet.HideColumns(5, 3);

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

C#/VB.NET Hide or Show Rows and Columns in Excel

Show All Hidden Rows and Columns in Excel in C# and VB.NET

The following steps demonstrate how to show all hidden rows and columns in Excel in C# and VB.NET:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
  • Iterate through the rows in the worksheet and find the hidden rows using Worksheet.GetRowIsHide(int rowIndex) method.
  • Show all hidden rows using Worksheet.ShowRow(int rowIndex) method.
  • Iterate through the columns in the worksheet and find the hidden columns using Worksheet.GetColumnIsHide(int columnIndex) method.
  • Show all hidden columns using Worksheet.ShowColumn(int columnIndex) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ShowAllHiddenRowsAndColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("HideRowsAndColumns.xlsx");

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

            //Iterate through the rows in the worksheet
            for (int i = 1; i <= sheet.LastRow; i++)
            {
                //Check if the current row is hidden
                if (sheet.GetRowIsHide(i))
                {
                    //Show the hidden row
                    sheet.ShowRow(i);
                }
            }

            //Iterate through the columns in the worksheet
            for (int j = 1; j <= sheet.LastRow; j++)
            {
                //Check if the current column is hidden
                if (sheet.GetColumnIsHide(j))
                {
                    //Show the hidden column
                    sheet.ShowColumn(j);
                }
            }

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

C#/VB.NET Hide or Show 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.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 08 November 2022 01:16