升级后发出Switch语句

问题描述 投票:0回答:1

我已将 DocumentFormat.OpenXml 升级到 3.0,0。升级我的 Excel 读取功能后抛出编译时错误。

这是获取Cell值的函数

   private static string GetCellValue(SpreadsheetDocument document, Cell cell)
        {
            DateTime ReleaseDate = new DateTime(1899, 12, 30);
            SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
            object value = string.Empty;
            CellFormats cellFormats = (CellFormats)document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats;
    
            string format = string.Empty; uint formatid = 0;
    
            if (cell.DataType == null)
            {
                CellFormat cf = new CellFormat();
                if (cell.StyleIndex == null)
                {
                    cf = cellFormats.Descendants<CellFormat>().ElementAt<CellFormat>(0);
                }
                else
                {
                    cf = cellFormats.Descendants<CellFormat>().ElementAt<CellFormat>(Convert.ToInt32(cell.StyleIndex.Value));
                }
    
                formatid = cf.NumberFormatId;
    
                if (cell != null && cell.InnerText.Length > 0)
                {
                    value = cell.CellValue.Text;
                    if (formatid > 13 && formatid <= 22)
                    {
                        DateTime answer = ReleaseDate.AddDays(Convert.ToDouble(cell.CellValue.Text));
                        value = answer.ToShortDateString();
                    }
    
                }
                else
                {
    
                    value = cell.InnerText;
                }
            }
    
            if (cell.DataType != null)
            {
                switch (cell.DataType.Value)
                {
                    case CellValues.SharedString:
                        return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(cell.CellValue.Text)].InnerText;
                    case CellValues.Boolean:
                        return cell.CellValue.Text == "1" ? "true" : "false";
                    case CellValues.Date:
                        {
                            DateTime answer = ReleaseDate.AddDays(Convert.ToDouble(cell.CellValue.Text));
                            return answer.ToShortDateString();
                        }
                    case CellValues.Number:
                        return Convert.ToDecimal(cell.CellValue.Text).ToString();
                    default:
                        if (cell.CellValue != null)
                            return cell.CellValue.Text;
                        return string.Empty;
                }
            }
    
            return value.ToString();
        }
      

我使用 Switch 功能时出错。

需要单元值类型的常量值

c# .net openxml documentformat
1个回答
0
投票

您不能对

switch
使用
readonly record struct
语句。根据 C# 8 文档:

如果 switch 表达式的类型是 sbyte、byte、short、ushort、int、uint、long、ulong、char、bool、string 或 enum_type,或者如果它是与这些类型之一对应的可为空值类型,那么这就是 switch 语句的控制类型。

您需要重构代码才能使用

if/else

if (cell.DataType.Value == CellValues.SharedString)
{
    return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(cell.CellValue.Text)].InnerText;
}
else
{
    ...
    ...
© www.soinside.com 2019 - 2024. All rights reserved.