Skip to content

Latest commit

 

History

History
110 lines (80 loc) · 4.08 KB

how-to-get-a-column-heading-in-a-spreadsheet.md

File metadata and controls

110 lines (80 loc) · 4.08 KB
api_name api_type ms.assetid title ms.suite ms.author author ms.topic ms.date ms.localizationpriority
Microsoft.Office.DocumentFormat.OpenXML.Packaging
schema
56ba8cee-d789-4a03-b8ff-b161af0788ff
How to: Get a column heading in a spreadsheet document
office
o365devx
o365devx
conceptual
01/03/2024
high

Get a column heading in a spreadsheet document

This topic shows how to use the classes in the Open XML SDK for Office to retrieve a column heading in a spreadsheet document programmatically.

[!includeStructure]

How the Sample Code Works

The code in this how-to consists of three methods (functions in Visual Basic): GetColumnHeading, GetColumnName, and GetRowIndex. The last two methods are called from within the GetColumnHeading method.

The GetColumnName method takes the cell name as a parameter. It parses the cell name to get the column name by creating a regular expression to match the column name portion of the cell name. For more information about regular expressions, see Regular Expression Language Elements.

[!code-csharp]

[!code-vb]


The GetRowIndex method takes the cell name as a parameter. It parses the cell name to get the row index by creating a regular expression to match the row index portion of the cell name.

[!code-csharp]

[!code-vb]


The GetColumnHeading method uses three parameters, the full path to the source spreadsheet file, the name of the worksheet that contains the specified column, and the name of a cell in the column for which to get the heading.

The code gets the name of the column of the specified cell by calling the GetColumnName method. The code also gets the cells in the column and orders them by row using the GetRowIndex method.

[!code-csharp]

[!code-vb]


If the specified column exists, it gets the first cell in the column using the IEnumerable(T).First method. The first cell contains the heading. Otherwise the specified column does not exist and the method returns null / Nothing

[!code-csharp]

[!code-vb]


If the content of the cell is stored in the SharedStringTablePart object, it gets the shared string items and returns the content of the column heading using the M:System.Int32.Parse(System.String) method. If the content of the cell is not in the SharedStringTable object, it returns the content of the cell.

[!code-csharp]

[!code-vb]


Sample Code

Following is the complete sample code in both C# and Visual Basic.

[!code-csharp]

[!code-vb]

See also

Open XML SDK class library reference