打开 xml sdk 2.0 使用公式读取 excel 单元格值

open xml sdk 2.0 read excel cell value with formula

我正在使用 openXML sdk 读取 excel 文件。一些 excel 单元格包含公式,我想读取单元格的值,但是当我使用下面的代码时,它正在为我获取公式。如何取值。

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

我在这里阅读包含公式的特定单元格 "D16"。

需要更多帮助,无法读取单元格的日期。我得到一些数字。 对于 Date 单元格数据类型为 Null,因此下面的代码不起作用。 如何从单元格中获取日期值

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

尝试 CellValue 单元格的属性。

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

我假设您正在对单元格引用进行硬编码以进行调试。否则我会强烈反对。

试试这个:

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