Skip to content

Commit 93c4e5c

Browse files
committed
clean up docs\spreadsheet\how-to-retrieve-the-values-of-cells-in-a-spreadsheet.md
1 parent 689bfba commit 93c4e5c

File tree

3 files changed

+70
-70
lines changed

3 files changed

+70
-70
lines changed

docs/spreadsheet/how-to-retrieve-the-values-of-cells-in-a-spreadsheet.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ ms.suite: office
1010
ms.author: o365devx
1111
author: o365devx
1212
ms.topic: conceptual
13-
ms.date: 11/20/2023
13+
ms.date: 01/10/2025
1414
ms.localizationpriority: high
1515
---
1616

1717
# Retrieve the values of cells in a spreadsheet document
1818

1919
This topic shows how to use the classes in the Open XML SDK for
2020
Office to programmatically retrieve the values of cells in a spreadsheet
21-
document. It contains an example **GetCellValue** method to illustrate
21+
document. It contains an example `GetCellValue` method to illustrate
2222
this task.
2323

2424

2525

2626
## GetCellValue Method
2727

28-
You can use the **GetCellValue** method to
28+
You can use the `GetCellValue` method to
2929
retrieve the value of a cell in a workbook. The method requires the
3030
following three parameters:
3131

@@ -62,7 +62,7 @@ initializes it to null.
6262
## Accessing the Cell
6363

6464
Next, the code opens the document by using the <xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open*> method, indicating that the document
65-
should be open for read-only access (the final **false** parameter). Next, the code retrieves a
65+
should be open for read-only access (the final `false` parameter). Next, the code retrieves a
6666
reference to the workbook part by using the <xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.WorkbookPart*> property of the document.
6767

6868
### [C#](#tab/cs-3)
@@ -111,8 +111,8 @@ the corresponding <xref:DocumentFormat.OpenXml.Spreadsheet.Worksheet.WorksheetPa
111111
Just as when locating the named sheet, when locating the named cell, the
112112
code uses the <xref:DocumentFormat.OpenXml.OpenXmlElement.Descendants*> method, searching for the first
113113
match in which the <xref:DocumentFormat.OpenXml.Spreadsheet.CellType.CellReference*> property equals the specified
114-
**addressName**
115-
parameter. After this method call, the variable named **theCell** will either contain a reference to the cell,
114+
`addressName`
115+
parameter. After this method call, the variable named `theCell` will either contain a reference to the cell,
116116
or will contain a null reference.
117117

118118
### [C#](#tab/cs-6)
@@ -125,9 +125,9 @@ or will contain a null reference.
125125

126126
## Retrieving the Value
127127

128-
At this point, the variable named **theCell**
128+
At this point, the variable named `theCell`
129129
contains either a null reference, or a reference to the cell that you
130-
requested. If you examine the Open XML content (that is, **theCell.OuterXml**) for the cell, you will find XML
130+
requested. If you examine the Open XML content (that is, `theCell.OuterXml`) for the cell, you will find XML
131131
such as the following.
132132

133133
```xml
@@ -151,9 +151,9 @@ Now, the sample method must interpret the value. As it is, the code
151151
handles numeric and date, string, and Boolean values. You can extend the
152152
sample as necessary. The <xref:DocumentFormat.OpenXml.Spreadsheet.Cell> type provides a
153153
<xref:DocumentFormat.OpenXml.Spreadsheet.CellType.DataType*> property that indicates the type
154-
of the data within the cell. The value of the **DataType** property is null for numeric and date
155-
types. It contains the value **CellValues.SharedString** for strings, and **CellValues.Boolean** for Boolean values. If the
156-
**DataType** property is null, the code returns
154+
of the data within the cell. The value of the `DataType` property is null for numeric and date
155+
types. It contains the value `CellValues.SharedString` for strings, and `CellValues.Boolean` for Boolean values. If the
156+
`DataType` property is null, the code returns
157157
the value of the cell (it is a numeric value). Otherwise, the code
158158
continues by branching based on the data type.
159159

@@ -165,7 +165,7 @@ continues by branching based on the data type.
165165
***
166166

167167

168-
If the **DataType** property contains **CellValues.SharedString**, the code must retrieve a
168+
If the `DataType` property contains `CellValues.SharedString`, the code must retrieve a
169169
reference to the single <xref:DocumentFormat.OpenXml.Packaging.WorkbookPart.SharedStringTablePart*>.
170170

171171
### [C#](#tab/cs-9)
@@ -178,7 +178,7 @@ reference to the single <xref:DocumentFormat.OpenXml.Packaging.WorkbookPart.Shar
178178

179179
Next, if the string table exists (and if it does not, the workbook is
180180
damaged and the sample code returns the index into the string table
181-
instead of the string itself) the code returns the **InnerText** property of the element it finds at the
181+
instead of the string itself) the code returns the `InnerText` property of the element it finds at the
182182
specified index (first converting the value property to an integer).
183183

184184
### [C#](#tab/cs-10)
@@ -189,7 +189,7 @@ specified index (first converting the value property to an integer).
189189
***
190190

191191

192-
If the **DataType** property contains **CellValues.Boolean**, the code converts the 0 or 1
192+
If the `DataType` property contains `CellValues.Boolean`, the code converts the 0 or 1
193193
it finds in the cell value into the appropriate text string.
194194

195195
### [C#](#tab/cs-11)
@@ -200,17 +200,18 @@ it finds in the cell value into the appropriate text string.
200200
***
201201

202202

203-
Finally, the procedure returns the variable **value**, which contains the requested information.
203+
Finally, the procedure returns the variable `value`, which contains the requested information.
204204

205205
## Sample Code
206206

207-
The following is the complete **GetCellValue** code sample in C\# and Visual Basic.
207+
The following is the complete `GetCellValue` code sample in C\# and Visual Basic.
208208

209209
### [C#](#tab/cs)
210210
[!code-csharp[](../../samples/spreadsheet/retrieve_the_values_of_cells/cs/Program.cs#snippet0)]
211211

212212
### [Visual Basic](#tab/vb)
213213
[!code-vb[](../../samples/spreadsheet/retrieve_the_values_of_cells/vb/Program.vb#snippet0)]
214+
***
214215

215216
## See also
216217

samples/spreadsheet/retrieve_the_values_of_cells/cs/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// <Snippet0>
21
using DocumentFormat.OpenXml.Packaging;
32
using DocumentFormat.OpenXml.Spreadsheet;
43
using System;
@@ -7,6 +6,7 @@
76
Console.WriteLine(GetCellValue(args[0], args[1], args[2]));
87
// Retrieve the value of a cell, given a file name, sheet name,
98
// and address name.
9+
// <Snippet0>
1010
// <Snippet1>
1111
static string GetCellValue(string fileName, string sheetName, string addressName)
1212
// </Snippet1>
@@ -88,9 +88,9 @@ static string GetCellValue(string fileName, string sheetName, string addressName
8888
break;
8989
default:
9090
value = "TRUE";
91-
// </Snippet11>
9291
break;
9392
}
93+
// </Snippet11>
9494
}
9595
}
9696
}
Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,91 @@
1-
' <Snippet0>
21
Imports DocumentFormat.OpenXml.Packaging
32
Imports DocumentFormat.OpenXml.Spreadsheet
43

54
Module Program
65
Sub Main(args As String())
6+
Console.WriteLine(GetCellValue(args(0), args(1), args(2)))
77
End Sub
88

9-
9+
' Retrieve the value of a cell, given a file name, sheet name,
10+
' and address name.
11+
' <Snippet0>
1012
' <Snippet1>
11-
Public Function GetCellValue(ByVal fileName As String, ByVal sheetName As String, ByVal addressName As String) As String
13+
Function GetCellValue(fileName As String, sheetName As String, addressName As String) As String
1214
' </Snippet1>
1315
' <Snippet2>
1416
Dim value As String = Nothing
1517
' </Snippet2>
1618
' <Snippet3>
1719
' Open the spreadsheet document for read-only access.
1820
Using document As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False)
19-
2021
' Retrieve a reference to the workbook part.
2122
Dim wbPart As WorkbookPart = document.WorkbookPart
2223
' </Snippet3>
2324
' <Snippet4>
24-
' Find the sheet with the supplied name, and then use that Sheet object
25-
' to retrieve a reference to the appropriate worksheet.
26-
Dim theSheet As Sheet = wbPart.Workbook.Descendants(Of Sheet)().
27-
Where(Function(s) s.Name = sheetName).FirstOrDefault()
25+
' Find the sheet with the supplied name, and then use that
26+
' Sheet object to retrieve a reference to the first worksheet.
27+
Dim theSheet As Sheet = wbPart?.Workbook.Descendants(Of Sheet)().Where(Function(s) s.Name = sheetName).FirstOrDefault()
2828

2929
' Throw an exception if there is no sheet.
30-
If theSheet Is Nothing Then
30+
If theSheet Is Nothing OrElse theSheet.Id Is Nothing Then
3131
Throw New ArgumentException("sheetName")
3232
End If
3333
' </Snippet4>
34-
' Retrieve a reference to the worksheet part.
3534
' <Snippet5>
35+
' Retrieve a reference to the worksheet part.
3636
Dim wsPart As WorksheetPart = CType(wbPart.GetPartById(theSheet.Id), WorksheetPart)
3737
' </Snippet5>
3838
' <Snippet6>
3939
' Use its Worksheet property to get a reference to the cell
4040
' whose address matches the address you supplied.
41-
Dim theCell As Cell = wsPart.Worksheet.Descendants(Of Cell).
42-
Where(Function(c) c.CellReference = addressName).FirstOrDefault
41+
Dim theCell As Cell = wsPart.Worksheet?.Descendants(Of Cell)().Where(Function(c) c.CellReference = addressName).FirstOrDefault()
4342
' </Snippet6>
4443
' <Snippet7>
4544
' If the cell does not exist, return an empty string.
46-
If theCell IsNot Nothing Then
47-
value = theCell.InnerText
48-
' </Snippet7>
49-
' <Snippet8>
50-
' If the cell represents an numeric value, you are done.
51-
' For dates, this code returns the serialized value that
52-
' represents the date. The code handles strings and
53-
' Booleans individually. For shared strings, the code
54-
' looks up the corresponding value in the shared string
55-
' table. For Booleans, the code converts the value into
56-
' the words TRUE or FALSE.
57-
If theCell.DataType IsNot Nothing Then
58-
Select Case theCell.DataType.Value
59-
' </Snippet8>
60-
Case CellValues.SharedString
61-
' <Snippet9>
62-
' For shared strings, look up the value in the
63-
' shared strings table.
64-
Dim stringTable = wbPart.GetPartsOfType(Of SharedStringTablePart).FirstOrDefault()
65-
' </Snippet9>
66-
' <Snippet10>
67-
' If the shared string table is missing, something
68-
' is wrong. Return the index that is in
69-
' the cell. Otherwise, look up the correct text in
70-
' the table.
71-
If stringTable IsNot Nothing Then
72-
value = stringTable.SharedStringTable.
73-
ElementAt(Integer.Parse(value)).InnerText
74-
End If
75-
' </Snippet10>
76-
' <Snippet11>
77-
Case CellValues.Boolean
78-
Select Case value
79-
Case "0"
80-
value = "FALSE"
81-
Case Else
82-
value = "TRUE"
83-
End Select
84-
' </Snippet11>
45+
If theCell Is Nothing OrElse theCell.InnerText.Length < 0 Then
46+
Return String.Empty
47+
End If
48+
value = theCell.InnerText
49+
' </Snippet7>
50+
' <Snippet8>
51+
' If the cell represents an integer number, you are done.
52+
' For dates, this code returns the serialized value that
53+
' represents the date. The code handles strings and
54+
' Booleans individually. For shared strings, the code
55+
' looks up the corresponding value in the shared string
56+
' table. For Booleans, the code converts the value into
57+
' the words TRUE or FALSE.
58+
If theCell.DataType IsNot Nothing Then
59+
If theCell.DataType.Value = CellValues.SharedString Then
60+
' </Snippet8>
61+
' <Snippet9>
62+
' For shared strings, look up the value in the
63+
' shared strings table.
64+
Dim stringTable = wbPart.GetPartsOfType(Of SharedStringTablePart)().FirstOrDefault()
65+
' </Snippet9>
66+
' <Snippet10>
67+
' If the shared string table is missing, something
68+
' is wrong. Return the index that is in
69+
' the cell. Otherwise, look up the correct text in
70+
' the table.
71+
If stringTable IsNot Nothing Then
72+
value = stringTable.SharedStringTable.ElementAt(Integer.Parse(value)).InnerText
73+
End If
74+
' </Snippet10>
75+
ElseIf theCell.DataType.Value = CellValues.Boolean Then
76+
' <Snippet11>
77+
Select Case value
78+
Case "0"
79+
value = "FALSE"
80+
Case Else
81+
value = "TRUE"
8582
End Select
83+
' </Snippet11>
8684
End If
8785
End If
8886
End Using
87+
8988
Return value
9089
End Function
90+
' </Snippet0>
9191
End Module
92-
' </Snippet0>

0 commit comments

Comments
 (0)