简体   繁体   中英

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;
                        }
                    }
                }

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.

Try this:

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();
    var value = theCell.CellValue.InnerText;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM