Search code examples
c#openxml-sdk

open xml sdk 2.0 read excel cell value with formula


I am reading excel file using openXML sdk. Some excel cells contains formula, I want to read the value of the cell but when I use the below code it is fetching me the formula. How to get the value.

WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
using(SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
  WorkbookPart wbPart = document.WorkbookPart;
  Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
  WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
  Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "D16").FirstOrDefault();
  value = theCell.InnerText;
}

Here I am reading particular cell "D16" which contains formula.

Need one more help, not able to read Date for the cell. I am getting some number. For Date cell Datatype is Null so below code not working. HOw do I get date vale from the cell

  if (theCell != null)
                {
                    value = theCell.CellValue.InnerText;
                    if (theCell.DataType != null)
                    {
                        switch (theCell.DataType.Value)
                        {
                            case CellValues.SharedString:
                                var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                                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;
                        }
                    }
                }

Solution

  • try CellValue proerty of the cell.

    WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
    using(SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
    {
      WorkbookPart wbPart = document.WorkbookPart;
      Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
      WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
      Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "D16").FirstOrDefault();
      value = theCell.CellValue;
    }
    

    I am assuming you are hardcoding the cell reference for debugging purposes. otherwise i would strongly advise against that.