api_name | api_type | ms.assetid | title | ms.suite | ms.author | author | ms.topic | ms.date | ms.localizationpriority | ||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
56ba8cee-d789-4a03-b8ff-b161af0788ff |
How to: Get a column heading in a spreadsheet document |
office |
o365devx |
o365devx |
conceptual |
01/03/2024 |
high |
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]
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.
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.
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.
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
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.
Following is the complete sample code in both C# and Visual Basic.