Skip to content

Commit f5c205d

Browse files
authored
Merge pull request #2054 from syncfusion-content/881834-FAQ-HideColumnsXlsIO
881834 - Add FAQ for hide columns using column name
2 parents 3303e0f + 2f39cad commit f5c205d

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

File-Formats-toc.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,7 @@
16881688
<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>
16891689
<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>
16901690
<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>
1691+
<li><a href="/file-formats/xlsio/faqs/how-to-hide-columns-using-column-name">How to hide columns using column name?</a></li>
16911692
</ul>
16921693
</li>
16931694
</ul>

File-Formats/XlsIO/FAQ.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@ The frequently asked questions in Essential XlsIO are listed below.
8282
* [How to find and replace text in hyperlinks?](faqs/how-to-find-and-replace-text-in-hyperlinks)
8383
* [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)
8484
* [How to set Logarithmic axis for chart in Excel document?](faqs/how-to-set-logarithmic-axis-for-chart-in-excel-document)
85-
* [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)
85+
* [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)
86+
* [How to hide columns using column name](faqs/how-to-hide-columns-using-column-name)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: How to hide columns using column name |Syncfusion.
3+
description: This page explains how to hide columns using column name using Syncfusion .NET Excel library (XlsIO).
4+
platform: file-formats
5+
control: XlsIO
6+
documentation: UG
7+
---
8+
9+
# How to hide columns using column name?
10+
11+
The following code illustrates how to hide columns using column name.
12+
13+
{% tabs %}
14+
{% highlight c# tabtitle="C# [Cross-platform]" %}
15+
using (ExcelEngine excelEngine = new ExcelEngine())
16+
{
17+
IApplication application = excelEngine.Excel;
18+
application.DefaultVersion = ExcelVersion.Xlsx;
19+
20+
//Loads an existing file
21+
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
22+
IWorkbook workbook = application.Workbooks.Open(inputStream);
23+
IWorksheet worksheet = workbook.Worksheets[0];
24+
25+
List<string> columnsToHide = new List<string> { "column1", "column2", "column3" };
26+
27+
foreach (string columnName in columnsToHide)
28+
{
29+
IRange headerCell = worksheet.UsedRange.FindFirst(columnName, ExcelFindType.Text);
30+
if (headerCell != null)
31+
{
32+
int columnIndex = headerCell.Column;
33+
34+
// Hide the column
35+
worksheet.ShowColumn(columnIndex, false);
36+
}
37+
}
38+
39+
//Saving the workbook as stream
40+
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
41+
workbook.SaveAs(stream);
42+
stream.Dispose();
43+
}
44+
{% endhighlight %}
45+
46+
{% highlight c# tabtitle="C# [Windows-specific]" %}
47+
using (ExcelEngine excelEngine = new ExcelEngine())
48+
{
49+
IApplication application = excelEngine.Excel;
50+
application.DefaultVersion = ExcelVersion.Xlsx;
51+
52+
//Loads an existing file
53+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
54+
IWorksheet worksheet = workbook.Worksheets[0];
55+
56+
List<string> columnsToHide = new List<string> { "column1", "column2", "column3" };
57+
58+
foreach (string columnName in columnsToHide)
59+
{
60+
IRange headerCell = worksheet.UsedRange.FindFirst(columnName, ExcelFindType.Text);
61+
if (headerCell != null)
62+
{
63+
int columnIndex = headerCell.Column;
64+
65+
// Hide the column
66+
worksheet.ShowColumn(columnIndex, false);
67+
}
68+
}
69+
70+
//Saving the workbook
71+
workbook.SaveAs("Output.xlsx");
72+
}
73+
{% endhighlight %}
74+
75+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
76+
Using excelEngine As ExcelEngine = New ExcelEngine()
77+
Dim application As IApplication = excelEngine.Excel
78+
application.DefaultVersion = ExcelVersion.Xlsx
79+
80+
' Loads an existing file
81+
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
82+
Dim worksheet As IWorksheet = workbook.Worksheets(0)
83+
84+
Dim columnsToHide As List(Of String) = New List(Of String) From {"column1", "column2", "column3"}
85+
86+
For Each columnName As String In columnsToHide
87+
Dim headerCell As IRange = worksheet.UsedRange.FindFirst(columnName, ExcelFindType.Text)
88+
If headerCell IsNot Nothing Then
89+
Dim columnIndex As Integer = headerCell.Column
90+
91+
' Hide the column
92+
worksheet.ShowColumn(columnIndex, False)
93+
End If
94+
Next
95+
96+
' Saving the workbook
97+
workbook.SaveAs("Output.xlsx")
98+
End Using
99+
{% endhighlight %}
100+
{% endtabs %}

File-Formats/XlsIO/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ using(ExcelEngine excelEngine = new ExcelEngine())
8181
//Remove the worksheet
8282
workbook.Worksheets[0].Remove();
8383

84-
//Saving the workbook as stream
84+
//Saving the workbook
8585
workbook.SaveAs("Output.xlsx");
8686
}
8787
{% endhighlight %}

0 commit comments

Comments
 (0)