Openxml单元格返回InnerText不是实际值

我开发一个可重用的代码来读取Excel单元格的值。 我的代码如下:

private string ReadCellValue(WorksheetPart worksheetPart, string cellAddress) { string value = null; Cell theCell = worksheetPart.Worksheet.Descendants<Cell>(). Where(c => c.CellReference == cellAddress).FirstOrDefault(); // If the cell does not exist, return an empty string. if (theCell != null) { value = theCell.InnerText; if (theCell.DataType != null) { switch (theCell.DataType.Value) { case CellValues.SharedString: var stringTable = worksheetPart.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; } } } return value; } 

我从下面的代码块调用这个方法:

 public string IsPackingListValid() { // 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("Could not find work sheet: " + sheetName); } // Retrieve a reference to the worksheet part. WorksheetPart worksheetPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id)); return ReadCellValue(WorksheetPart worksheetPart, "B2") } } 

该方法不返回某些单元格的实际值。 对于某些单元格,它返回值,对于某些单元格,它将返回内部文本。 我打了个澡,检查了一下

 var stringTable = worksheetPart.GetPartsOfType<SharedStringTablePart>() .FirstOrDefault(); 

上面的代码返回null。

我找不到为什么它的一些细胞工作,而不是为一些工作。 任何有助于解决这个问题。

从我的理解(至less,我已经检查过它是真的),SharedStringTablePart是WorkbookPart的后代,而不是WorksheetPart。 所以你可以做什么来修复你的代码是从IsPackingListValid()方法中的wbPart获得共享string列表:

 List<SharedStringItem> sharedStrings = wbPart.SharedStringTablePart.SharedStringTable.ChildElements.OfType<SharedStringItem>().ToList(); 

然后作为附加parameter passing给ReadCellValue方法:

 return ReadCellValue(worksheetPart, "B2", sharedStrings); 

之后,您可以在ReadCellValue()方法中获得交换机内共享string的实际值:

 value = cell.InnerText; int sharedStringIndex; if (int.TryParse(cell.InnerText, out sharedStringIndex) && sharedStrings.Count > sharedStringIndex && sharedStrings[sharedStringIndex].Text != null) { value = sharedStrings[sharedStringIndex].Text.Text; } 

还需要在SharedString数据types上进行额外检查( http://msdn.microsoft.com/zh-cn/library/office/ff921204(v=office.14).aspx以获取更多信息):

  private static string ReadCellValue(WorksheetPart worksheetPart, string cellAddress, List<SharedStringItem> sharedStrings) { try { string value = null; var cell = worksheetPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == cellAddress).FirstOrDefault(); value = cell.InnerText; if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) { int sharedStringIndex; if (int.TryParse(cell.InnerText, out sharedStringIndex) && sharedStrings.Count > sharedStringIndex && sharedStrings[sharedStringIndex].Text != null) { value = sharedStrings[sharedStringIndex].Text.Text; } } return value; } catch (Exception ex) { throw ex; } }