Created
July 15, 2016 08:30
-
-
Save akrisiun/eda6243d66fa3b9f5179640ba7b39ab6 to your computer and use it in GitHub Desktop.
DocumentFormat.OpenXml GetCellValue.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DocumentFormat.OpenXml::GetCellValue.cs | |
https://msdn.microsoft.com/en-us/library/office/hh298534.aspx?f=255&MSPPError=-2147217396 | |
// Retrieve the value of a cell, given a file name, sheet name, | |
// and address name. | |
public static string GetCellValue(string fileName, | |
string sheetName, | |
string addressName) | |
{ | |
string value = null; | |
// Open the spreadsheet document for read-only access. | |
using (SpreadsheetDocument document = | |
SpreadsheetDocument.Open(fileName, false)) | |
{ | |
// Retrieve a reference to the workbook part. | |
WorkbookPart wbPart = document.WorkbookPart; | |
// Find the sheet with the supplied name, and then use that | |
// Sheet object to retrieve a reference to the first worksheet. | |
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>(). | |
Where(s => s.Name == sheetName).FirstOrDefault(); | |
// Throw an exception if there is no sheet. | |
if (theSheet == null) | |
{ | |
throw new ArgumentException("sheetName"); | |
} | |
// Retrieve a reference to the worksheet part. | |
WorksheetPart wsPart = | |
(WorksheetPart)(wbPart.GetPartById(theSheet.Id)); | |
// Use its Worksheet property to get a reference to the cell | |
// whose address matches the address you supplied. | |
Cell theCell = wsPart.Worksheet.Descendants<Cell>(). | |
Where(c => c.CellReference == addressName).FirstOrDefault(); | |
// If the cell does not exist, return an empty string. | |
if (theCell != null) | |
{ | |
value = theCell.InnerText; | |
// If the cell represents an integer number, you are done. | |
// For dates, this code returns the serialized value that | |
// represents the date. The code handles strings and | |
// Booleans individually. For shared strings, the code | |
// looks up the corresponding value in the shared string | |
// table. For Booleans, the code converts the value into | |
// the words TRUE or FALSE. | |
if (theCell.DataType != null) | |
{ | |
switch (theCell.DataType.Value) | |
{ | |
case CellValues.SharedString: | |
// For shared strings, look up the value in the | |
// shared strings table. | |
var stringTable = | |
wbPart.GetPartsOfType<SharedStringTablePart>() | |
.FirstOrDefault(); | |
// If the shared string table is missing, something | |
// is wrong. Return the index that is in | |
// the cell. Otherwise, look up the correct text in | |
// the table. | |
if (stringTable != null) | |
{ | |
value = | |
stringTable.SharedStringTable | |
.ElementAt(int.Parse(value)).InnerText; | |
} | |
break; | |
case CellValues.Boolean: | |
switch (value) | |
{ | |
case "0": | |
value = "FALSE"; | |
break; | |
default: | |
value = "TRUE"; | |
break; | |
} | |
break; | |
} | |
} | |
} | |
} | |
return value; | |
} | |
/* | |
const string fileName = | |
@"C:\users\public\documents\RetrieveCellValue.xlsx"; | |
// Retrieve the value in cell A1. | |
string value = GetCellValue(fileName, "Sheet1", "A1"); | |
Console.WriteLine(value); | |
// Retrieve the date value in cell A2. | |
value = GetCellValue(fileName, "Sheet1", "A2"); | |
Console.WriteLine( | |
DateTime.FromOADate(double.Parse(value)).ToShortDateString()); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment