Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active November 15, 2021 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/c8b7bf4199a0f3276845b27d310d7b43 to your computer and use it in GitHub Desktop.
Save conholdate-gists/c8b7bf4199a0f3276845b27d310d7b43 to your computer and use it in GitHub Desktop.
Hide or Show Rows and Columns in Excel using C#
// Instantiate a workbook
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Hiding the 3rd row of the worksheet
worksheet.Cells.HideRow(2);
// Hiding the 2nd column of the worksheet
worksheet.Cells.HideColumn(1);
// Saving the modified Excel file
workbook.Save(@"C:\Files\HideRowsColumns.xlsX");
// Instantiate a Workbook
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Hiding 3,4 and 5 rows in the worksheet
worksheet.Cells.HideRows(2, 3);
// Hiding 2 and 3 columns in the worksheet
worksheet.Cells.HideColumns(1, 2);
// Saving the modified Excel file
workbook.Save(@"C:\Files\HideMultiple.xlsx");
// Instantiate a workbook
Workbook workbook = new Workbook(@"C:\Files\HideRowsColumns.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Unhiding the 3rd row and setting its height to 13.5
worksheet.Cells.UnhideRow(2, 13.5);
// Unhiding the 2nd column and setting its width to 8.5
worksheet.Cells.UnhideColumn(1, 20.5);
// Saving the modified Excel file
workbook.Save(@"C:\Files\ShowRowsColumns.xlsx");
// Instantiate a Workbook
Workbook workbook = new Workbook(@"C:\Files\HideMultiple.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Show all rows
var AllRows = worksheet.Cells.Rows;
foreach (Row row in AllRows)
{
if (row.IsHidden)
{
worksheet.Cells.UnhideRow(row.Index, 20.5);
}
}
// Show all columns
var AllColumns = worksheet.Cells.Columns;
foreach (var column in AllColumns)
{
if (column.IsHidden)
{
worksheet.Cells.UnhideColumn(column.Index, 20.5);
}
}
// Saving the modified Excel file
workbook.Save(@"C:\Files\ShowAllRowsColumns.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment