C# OpenXML 确定值是数字还是在共享字符串表中

C# OpenXML determining if a value is a number or in sharedstringtable

我的程序需要扫描 excel 文档并获取特定单元格中的值并将它们放入列表中。

然而,电子表格中的数据都是一般格式,并且总是在 SST 中查找值,我想无论如何,并没有将电子表格中的任何数值放入列表中。

如何告诉我的程序数据是一个数字而不是对 SST 的引用?

foreach (Cell cell in row.Elements<Cell>())
{
    try
    {
        cellvalue1 = cell.CellValue.InnerText;
        if (cell.DataType == CellValues.SharedString && cellvalue2.Any(char.IsDigit))
        {
            cellvalue2 = ssT.ElementAt(Int32.Parse(cellvalue1)).InnerText;
        }
        else 
        {
            cellvalue2 = cell.CellValue.ToString();
        }
    }
    catch (Exception)
    {
        cellvalue2 = " ";
    }                                    

    switch (cellvalue2)
    {
        case ("WELL NAME and NUMBER"):
            WellnameCol = GetColumnName(cell.CellReference);                                                                                                           
            break;
        case ("FLOWING PRESSURE"):
            FlowpCol = GetColumnName(cell.CellReference);
            break;
        case ("SHUT-IN PRESSURE"):
            ShutpCol = GetColumnName(cell.CellReference);
            break;
        default:
            if (GetColumnName(cell.CellReference) == WellnameCol)
            {
                if (cellvalue2.Contains("#"))
                {
                    Wellname.Add(cellvalue2);
                    inRow = true;
                }
                else
                {
                    inRow = false;
                }                                                                                                
            }    
            else if (GetColumnName(cell.CellReference) == FlowpCol)
            {   
                 if (!cellvalue2.Contains("#") && inRow)
                     Flowp.Add(cellvalue2);
            }
            else if (GetColumnName(cell.CellReference) == ShutpCol)
            {   
                 if (inRow)
                 {
                     ShutP.Add(cellvalue2);
                 }
            }
            break;                                           
    }                                                                       
}

Try Catch 语句是判断单元格是否为空,然后returns如果是,则单元格为空字符串。

感谢所有帮助。

我创建了一个小 Excel 文件,其中一个单元格格式为字符串,文本为“1234”,第二个单元格文本内容为 "abcd"。

通过检查 xlsx 文件的内容,我看到了它们的 xml 代码。

<c r="A1" s="1"><v>1234</v></c>
<c r="A2" t="s"><v>0</v></c>

第二个单元格t="s"表示共享字符串。对于第一个,没有给出明确的数据类型(t 属性)。此 MSDN 示例中也使用了这种区分。 https://msdn.microsoft.com/de-de/library/office/hh298534.aspx这是您的问题的有趣部分:

        value = theCell.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;
            }
        }

希望对您有所帮助。

我过去做过类似的事情来从 excel 电子表格中提取数据,下面是我用来检索单元格值的特定代码:

public static string GetCellValue(SharedStringTable sharedStringTable, Cell cell)
{
    string value = cell.CellValue.InnerText;

    if (cell.DataType != null
        && cell.DataType.Value == CellValues.SharedString)
    {
        return sharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
    }
    else
    {
        return value;
    }
}