Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions File-Formats-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,7 @@
<li><a href="/file-formats/xlsio/faqs/how-to-fix-the-argument-out-of-range-exception-when-accessing-a-large-number-of-rows-and-columns">How to fix the ArgumentOutOfRangeException when accessing a large number of rows and columns?</a></li>
<li><a href="/file-formats/xlsio/faqs/how-to-set-logarithmic-axis-for-chart-in-excel-document">How to set Logarithmic axis for chart in Excel document?</a></li>
<li><a href="/file-formats/xlsio/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows">How to resolve performance issue when deleting a large number of rows?</a></li>
<li><a href="/file-formats/xlsio/faqs/how-to-hide-columns-using-column-name">How to hide columns using column name?</a></li>
</ul>
</li>
</ul>
Expand Down
3 changes: 2 additions & 1 deletion File-Formats/XlsIO/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ The frequently asked questions in Essential XlsIO are listed below.
* [How to find and replace text in hyperlinks?](faqs/how-to-find-and-replace-text-in-hyperlinks)
* [How to fix the ArgumentOutOfRangeException when accessing a large number of rows and columns?](faqs/how-to-fix-the-argument-out-of-range-exception-when-accessing-a-large-number-of-rows-and-columns)
* [How to set Logarithmic axis for chart in Excel document?](faqs/how-to-set-logarithmic-axis-for-chart-in-excel-document)
* [How to resolve performance issue when deleting a large number of rows?](faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows)
* [How to resolve performance issue when deleting a large number of rows?](faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows)
* [How to hide columns using column name](faqs/how-to-hide-columns-using-column-name)
100 changes: 100 additions & 0 deletions File-Formats/XlsIO/faqs/how-to-hide-columns-using-column-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
title: How to hide columns using column name |Syncfusion.
description: This page explains how to hide columns using column name using Syncfusion .NET Excel library (XlsIO).
platform: file-formats
control: XlsIO
documentation: UG
---

# How to hide columns using column name?

The following code illustrates how to hide columns using column name.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Loads an existing file
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];

List<string> columnsToHide = new List<string> { "column1", "column2", "column3" };

foreach (string columnName in columnsToHide)
{
IRange headerCell = worksheet.UsedRange.FindFirst(columnName, ExcelFindType.Text);
if (headerCell != null)
{
int columnIndex = headerCell.Column;

// Hide the column
worksheet.ShowColumn(columnIndex, false);
}
}

//Saving the workbook as stream
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(stream);
stream.Dispose();
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Loads an existing file
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];

List<string> columnsToHide = new List<string> { "column1", "column2", "column3" };

foreach (string columnName in columnsToHide)
{
IRange headerCell = worksheet.UsedRange.FindFirst(columnName, ExcelFindType.Text);
if (headerCell != null)
{
int columnIndex = headerCell.Column;

// Hide the column
worksheet.ShowColumn(columnIndex, false);
}
}

//Saving the workbook
workbook.SaveAs("Output.xlsx");
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx

' Loads an existing file
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)

Dim columnsToHide As List(Of String) = New List(Of String) From {"column1", "column2", "column3"}

For Each columnName As String In columnsToHide
Dim headerCell As IRange = worksheet.UsedRange.FindFirst(columnName, ExcelFindType.Text)
If headerCell IsNot Nothing Then
Dim columnIndex As Integer = headerCell.Column

' Hide the column
worksheet.ShowColumn(columnIndex, False)
End If
Next

' Saving the workbook
workbook.SaveAs("Output.xlsx")
End Using
{% endhighlight %}
{% endtabs %}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ using(ExcelEngine excelEngine = new ExcelEngine())
//Remove the worksheet
workbook.Worksheets[0].Remove();

//Saving the workbook as stream
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}
{% endhighlight %}
Expand Down