From 2f39cadc16c2245a3223bc5f9b3cdf7958732f6b Mon Sep 17 00:00:00 2001 From: KurmithaSF4004 Date: Mon, 15 Apr 2024 18:10:24 +0530 Subject: [PATCH] 881834-FAQ-HideColumnsXlsIO --- File-Formats-toc.html | 1 + File-Formats/XlsIO/FAQ.md | 3 +- .../how-to-hide-columns-using-column-name.md | 100 ++++++++++++++++++ ...ue-when-deleting-a-large-number-of-rows.md | 2 +- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 File-Formats/XlsIO/faqs/how-to-hide-columns-using-column-name.md diff --git a/File-Formats-toc.html b/File-Formats-toc.html index f5548652e..f2a8476db 100644 --- a/File-Formats-toc.html +++ b/File-Formats-toc.html @@ -1688,6 +1688,7 @@
  • How to fix the ArgumentOutOfRangeException when accessing a large number of rows and columns?
  • How to set Logarithmic axis for chart in Excel document?
  • How to resolve performance issue when deleting a large number of rows?
  • +
  • How to hide columns using column name?
  • diff --git a/File-Formats/XlsIO/FAQ.md b/File-Formats/XlsIO/FAQ.md index fb3c5eb10..480f40187 100644 --- a/File-Formats/XlsIO/FAQ.md +++ b/File-Formats/XlsIO/FAQ.md @@ -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) \ No newline at end of file +* [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) \ No newline at end of file diff --git a/File-Formats/XlsIO/faqs/how-to-hide-columns-using-column-name.md b/File-Formats/XlsIO/faqs/how-to-hide-columns-using-column-name.md new file mode 100644 index 000000000..5bf40b699 --- /dev/null +++ b/File-Formats/XlsIO/faqs/how-to-hide-columns-using-column-name.md @@ -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 columnsToHide = new List { "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 columnsToHide = new List { "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 %} diff --git a/File-Formats/XlsIO/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows.md b/File-Formats/XlsIO/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows.md index f4bfbc461..3a831614f 100644 --- a/File-Formats/XlsIO/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows.md +++ b/File-Formats/XlsIO/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows.md @@ -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 %}