|
| 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 %} |
0 commit comments