我已经将DocumentFormat.OpenXml升级到3.0,0.升级后我的EXCEL读取函数抛出编译时间错误.

100

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 功能时出错.

应为单元格值类型的常量值

enter image description here

推荐答案

readonly record struct不能使用switch语句.根据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
{
    ...
    ...

Csharp相关问答推荐

如何将ref T*重新解释为ref nint?

MongoDB实体框架核心:表达必须可写

有没有一种方法可以防止在编译时在MicrosoftC或非单线程上下文中调用方法?

为什么EF Core 6会针对null验证Count(*)?

数组被内部函数租用时,如何将数组返回给ArrayPool?

Microsoft. VisualBasic. FileIO. FileSystem. MoveFile()对话框有错误?

如何注销Microsoft帐户?

.NET 8 Web-API返回空列表

C#使用TextFieldParser读取.csv,但无法使用";0";替换创建的列表空条目

异步任务导致内存泄漏

DateTime ToString()未以指定格式打印

如何管理Azure认证客户端响应和证书 fingerprint

在try 使用访问服务器上的文件夹时,如何解决CORS错误.NET核心API

无法使用[FromForm]发送带有图像和JSON的多部分请求

.Net MAUI,在将FlyoutPage添加到容器之前,必须设置添加构造函数导致Flyout和Detail"

如何使用.NET Aspire从Blazor应用程序与GRPC API通信?

C#USB条形码 scanner 在第二次扫描时未写入行尾字符

无法向Unity注册Microsoft Logger

try 访问字典中的模拟对象时引发KeyNotFoundException

如何对正方形格线进行对角分组