List of Excel outline groups in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can group or ungroup the rows and columns and get the list of all outline groups, in an Excel worksheet.
Steps to get the list of all outline groups in Excel worksheet, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application project
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
A group in an Excel worksheet in represented by OutlineWrapper class and OutlineWrappers property of WorksheetImpl will have the collection of groups in the worksheet.
Step 3: Add an Excel file to you project and make it an embedded resource using the following steps.
- In Visual Studio, click the Project menu and select Add Existing Item. Find and select the Excel file you want to add to your project.
- In the Solution Explorer window, right-click on the Excel file you just added to your project and select Properties from the popup menu. The Properties tool window appears.
- In the Properties window, change the Build Action property to Embedded Resource.
- Build the project. The Excel file will be compiled into your project’s assembly.
Step 4: Include the following namespaces in Program.cs file.
C#
using Syncfusion.XlsIO; using Syncfusion.XlsIO.Implementation; using System; using System.Collections.Generic; using System.IO; using System.Reflection;
VB.NET
Imports Syncfusion.XlsIO Imports Syncfusion.XlsIO.Implementation Imports System.IO Imports System.Reflection
Step 5: Include the following code snippet in main method of Program.cs file to get the list of all outline groups in Excel worksheet.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Load the existing Excel document into IWorkbook Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream workbookStream = assembly.GetManifestResourceStream("OutlineGroups.monthly_sales.xlsx"); IWorkbook workbook = application.Workbooks.Open(workbookStream); //Get the first worksheet in the workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; //Instantiate a list to maintain the outline groups List<OutlineWrapper> outlineGrps = new List<OutlineWrapper>(); //Get the outline group ranges using OutlineWrappers foreach (OutlineWrapper outline in (worksheet as WorksheetImpl).OutlineWrappers) { //Add the outline group into list outlineGrps.Add(outline); } //Print the details of outline groups in console window Console.WriteLine("Total number of outline groups in the worksheet are : " + outlineGrps.Count + "\n"); for (int i = 0; i < outlineGrps.Count; i++) { Console.WriteLine("Address of the Outline Group - " + (i + 1).ToString() + " is " + outlineGrps[i].OutlineRange.Address); } Console.ReadLine(); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Load the existing Excel document into IWorkbook Dim assembly As Assembly = GetType(Module1).GetTypeInfo.Assembly Dim workbookStream As Stream = assembly.GetManifestResourceStream("OutlineGroup.monthly_sales.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(workbookStream) 'Get the first worksheet in the workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Instantiate a list to maintain the outline groups Dim outlineGrps As List(Of OutlineWrapper) = New List(Of OutlineWrapper) 'Get the outline group ranges using OutlineWrappers For Each outline As OutlineWrapper In CType(worksheet, WorksheetImpl).OutlineWrappers 'Add the outline group into list outlineGrps.Add(outline) Next 'Print the details of outline groups in console window Console.WriteLine("Total number of outline groups in the worksheet are : " + outlineGrps.Count.ToString() + "" & vbLf) For i As Integer = 0 To outlineGrps.Count - 1 Step 1 Console.WriteLine("Address of the Outline Group - " + (i + 1).ToString + " is " + outlineGrps(i).OutlineRange.Address) Next Console.ReadLine() End Using
A complete working sample to get the list of all outline groups in Excel worksheet can be downloaded from Outline-Groups.zip.
By executing the program, you will get the console window with list of all Outline Groups in worksheet as follows.
List of all outline groups in Excel worksheet
Take a moment to peruse the documentation where you will find other options like insertion and deletion of rows and columns, show or hide rows and columns, show or hide specific range, adjust row height and column width, autofit rows and columns and group or ungroup rows and columns with code examples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.