How to detect hidden columns in .NET Excel Library
16 Aug 20263 minutes to read
You can determine whether a column is hidden by inspecting the worksheet’s column information. The example below uses WorksheetImpl to access the ColumnInformation collection and checks the IsHidden property for the requested column index.
Note: column indices in ColumnInformation are 1-based.
The following examples show the pattern in C# (cross-platform and Windows-specific) and VB.NET.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
// Use the concrete WorksheetImpl when you need access to implementation-specific members
WorksheetImpl sheet = workbook.Worksheets[0] as WorksheetImpl;
// Hide column 1
sheet.ShowColumn(1, false);
// Detect whether column 1 is hidden
bool hidden = sheet.ColumnInformation[1] != null && sheet.ColumnInformation[1].IsHidden;
Console.WriteLine($"Column 1 hidden: {hidden}");
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
WorksheetImpl sheet = workbook.Worksheets[0] as WorksheetImpl;
// Hide column 1
sheet.ShowColumn(1, false);
// Detect whether column 1 is hidden
bool hidden = sheet.ColumnInformation[1] != null && sheet.ColumnInformation[1].IsHidden;
Console.WriteLine($"Column 1 hidden: {hidden}");
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
' Use the concrete WorksheetImpl when you need access to implementation-specific members
Dim sheet As WorksheetImpl = TryCast(workbook.Worksheets(0), WorksheetImpl)
' Hide column 1
sheet.ShowColumn(1, False)
' Detect whether column 1 is hidden
Dim hidden As Boolean = sheet.ColumnInformation(1) IsNot Nothing AndAlso sheet.ColumnInformation(1).IsHidden
Console.WriteLine($"Column 1 hidden: {hidden}")
workbook.SaveAs("Output.xlsx")
End UsingA complete working example in C# is present on this GitHub page.