Excel工作表中的所有字符串都存储在类似SharedStringTable的数组中。该表的目标是将所有字符串集中在基于索引的数组中,然后在文档中多次使用该字符串以仅引用此数组中的索引的情况。就是说,当您获得A1单元格的文本值时收到的0是SharedStringTable的索引。要获得真正的价值,您可以使用以下帮助函数:
public static SharedStringItem GetSharedStringItemById(WorkbookPart workbookPart, int id){ return workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(id);}然后在您的代码中像这样调用它以获取实际价值:
Cell cell = GetCell(worksheet, "A", 1);string cellValue = string.Empty;if (cell.DataType != null){ if (cell.DataType == CellValues.SharedString) { int id = -1; if (Int32.TryParse(cell.InnerText, out id)) {SharedStringItem item = GetSharedStringItemById(workbookPart, id);if (item.Text != null){ cellValue = item.Text.Text;}else if (item.InnerText != null){ cellValue = item.InnerText;}else if (item.InnerXml != null){ cellValue = item.InnerXml;} } }}


