Hide or Show Rows and Columns in Excel using C#

As a C# developer, you can programmatically hide or show rows and columns in Excel worksheets. This article shows how to hide and show rows or columns of an Excel sheet using C#.

The following topics are covered:

C# API to Hide and Show Rows or Columns

To hide and show rows and columns in an Excel sheet, we use Aspose.Cells for .NET API. This popular spreadsheet library lets you create and manipulate Excel files from .NET applications. It provides methods to hide any row or column and to reveal hidden rows and columns programmatically.

You can either download the DLL or install it via NuGet.

Install-Package Aspose.Cells

Hide Rows and Columns using C#

Follow these steps to hide rows and columns:

  • Create a Workbook instance with the input file path.
  • Get a Worksheet object.
  • Access the worksheet from the Worksheets collection by index.
  • Hide a row with the HideRow() method, passing the row index.
  • Hide a column with the HideColumn() method, passing the column index.
  • Save the file using the Save() method.

The code sample below demonstrates how to hide rows and columns in an Excel sheet using C#.

Hide Rows and Columns using C#

Hide Rows and Columns using C#.

The Workbook class represents an Excel workbook. Its Worksheets property provides access to all worksheets. The Worksheet class represents a single sheet, and its Cells property gives access to the cell collection.

The HideRow() method of the Cells class hides a specific row by index. The same class offers the HideColumn() method to hide a column by index. Finally, the Save() method writes the workbook to the specified file path.

Show Hidden Rows and Columns using C#

To reveal hidden rows and columns, use these steps:

  • Create a Workbook instance with the input file path.
  • Get a Worksheet object.
  • Access the worksheet from the Worksheets collection by index.
  • Unhide a row with the UnhideRow() method, providing the row index and desired height.
  • Unhide a column with the UnhideColumn() method, providing the column index and desired width.
  • Save the file using the Save() method.

The code sample below shows how to show specific hidden rows and columns in an Excel sheet using C#.

Show Hidden Rows and Columns using C#

Show Hidden Rows and Columns using C#

The UnhideRow() method of the Cells class makes a hidden row visible. The UnhideColumn() method does the same for a hidden column.

Hide Multiple Rows and Columns using C#

To hide several rows and columns at once:

  • Create a Workbook instance with the input file path.
  • Get a Worksheet object.
  • Access the worksheet from the Worksheets collection by index.
  • Call HideRows() with the start row index and the number of rows to hide.
  • Call HideColumns() with the start column index and the number of columns to hide.
  • Save the workbook with the Save() method.

The code sample below demonstrates how to hide multiple rows and columns in an Excel sheet using C#.

Hide Multiple Rows and Columns using C#

Hide Multiple Rows and Columns using C#.

The HideRows() method hides a range of rows, while HideColumns() hides a range of columns based on the supplied start index and count.

Show All Hidden Rows and Columns using C#

To reveal every hidden row and column:

  • Create a Workbook instance with the input file path.
  • Get a Worksheet object.
  • Access the worksheet from the Worksheets collection by index.
  • Loop through rows, check the IsHidden property, and call UnhideRow() with the row index and height when true.
  • Loop through columns, check the IsHidden property, and call UnhideColumn() with the column index and width when true.
  • Save the workbook using the Save() method.

The code sample below shows how to show all hidden rows and columns in an Excel sheet using C#.

Show All Hidden Rows and Columns using C#

Show All Hidden Rows and Columns using C#.

The IsHidden property of the Row class indicates if a row is hidden. The IsHidden property of the Column class does the same for columns.

Get a Free License

Request a free temporary license to use the API without evaluation limits.

Conclusion

In this article you learned how to hide columns and rows in Excel files using C#. You also saw how to show hidden columns and rows, how to hide multiple rows and columns, and how to reveal all hidden rows and columns. Explore more about Aspose.Cells for .NET in the documentation. For questions, visit the forum.

See Also