我正在开发一个可重用的代码来读取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 = …Run Code Online (Sandbox Code Playgroud)