How to: Retrieve the values of cells in a spreadsheet document (Open XML SDK)
Last modified: March 22, 2013
Applies to: Office 2013 | Open XML
In this article
GetCellValue Method
Calling the GetCellValue Sample Method
How the Code Works
Accessing the Cell
Retrieving the Value
Sample Code
This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically retrieve the values of cells in a spreadsheet document. It contains an example GetCellValue method to illustrate this task.
To use the sample code in this topic, you must install the Open XML SDK 2.5. You must explicitly reference the following assemblies in your project:
-
WindowsBase
-
DocumentFormat.OpenXml (Installed by the Open XML SDK)
You must also use the following using directives or Imports statements to compile the code in this topic.
You can use the GetCellValue method to retrieve the value of a cell in a workbook. The method requires the following three parameters:
-
A string that contains the name of the document to examine.
-
A string that contains the name of the sheet to examine.
-
A string that contains the cell address (such as A1, B12) from which to retrieve a value.
The method returns the value of the specified cell, if it could be found. The following code example shows the method signature.
Next, the code opens the document by using the Open method, indicating that the document should be open for read-only access (the final false parameter). Next, the code retrieves a reference to the workbook part by using the WorkbookPart property of the document.
To find the requested cell, the code must first retrieve a reference to the sheet, given its name. The code must search all the sheet-type descendants of the workbook part workbook element and examine the Name property of each sheet that it finds. Be aware that this search looks through the relations of the workbook, and does not actually find a worksheet part. It finds a reference to a Sheet, which contains information such as the name and Id of the sheet. The simplest way to do this is to use a LINQ query, as shown in the following code example.
Be aware that the FirstOrDefault method returns either the first matching reference (a sheet, in this case) or a null reference if no match was found. The code checks for the null reference, and throws an exception if you passed in an invalid sheet name.Now that you have information about the sheet, the code must retrieve a reference to the corresponding worksheet part. The sheet information that you already retrieved provides an Id property, and given that Id property, the code can retrieve a reference to the corresponding WorksheetPart by calling the workbook part GetPartById method.
Just as when locating the named sheet, when locating the named cell, the code uses the Descendants method, searching for the first match in which the CellReference property equals the specified addressName parameter. After this method call, the variable named theCell will either contain a reference to the cell, or will contain a null reference.
At this point, the variable named theCell contains either a null reference, or a reference to the cell that you requested. If you examine the Open XML content (that is, theCell.OuterXml) for the cell, you will find XML such as the following.
<x:c r="A1">
<x:v>12.345000000000001</x:v>
</x:c>
The InnerText property contains the content for the cell, and so the next block of code retrieves this value.
Now, the sample method must interpret the value. As it is, the code handles numeric and date, string, and Boolean values. You can extend the sample as necessary. The Cell type provides a DataType property that indicates the type of the data within the cell. The value of the DataType property is null for numeric and date types. It contains the value CellValues.SharedString for strings, and CellValues.Boolean for Boolean values. If the DataType property is null, the code returns the value of the cell (it is a numeric value). Otherwise, the code continues by branching based on the data type.
If the DataType property contains CellValues.SharedString, the code must retrieve a reference to the single SharedStringTablePart.
Next, if the string table exists (and if it does not, the workbook is damaged and the sample code returns the index into the string table instead of the string itself) the code returns the InnerText property of the element it finds at the specified index (first converting the value property to an integer).
If the DataType property contains CellValues.Boolean, the code converts the 0 or 1 it finds in the cell value into the appropriate text string.
Finally, the procedure returns the variable value, which contains the requested information.
|
Contribute to this article Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub. |