You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级DocumentFormat.OpenXml 3.0.0后switch语句编译报错

DocumentFormat.OpenXml 3.0.0升级后Excel读取函数编译错误修复

升级DocumentFormat.OpenXml至3.0.0版本后,Excel读取函数的switch语句出现编译错误,错误提示:

需要CellValues类型的常量值

原出错代码片段

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;
    }
}

问题原因

DocumentFormat.OpenXml 3.0.0对CellValues做了核心变更:它不再是枚举类型,改为包含静态字符串常量的类。而switch的case标签要求必须是编译时常量,现在CellValues.SharedString这类成员是字符串字段,无法满足switch的常量要求,因此触发编译错误。

修复方案

将switch语句替换为if-else条件判断,直接对比cell.DataType.Value的字符串值即可适配新版本的API变更。

修改后的完整函数代码

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)
    {
        string dataTypeValue = cell.DataType.Value;
        if (dataTypeValue == CellValues.SharedString)
        {
            return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(cell.CellValue.Text)].InnerText;
        }
        else if (dataTypeValue == CellValues.Boolean)
        {
            return cell.CellValue.Text == "1" ? "true" : "false";
        }
        else if (dataTypeValue == CellValues.Date)
        {
            DateTime answer = ReleaseDate.AddDays(Convert.ToDouble(cell.CellValue.Text));
            return answer.ToShortDateString();
        }
        else if (dataTypeValue == CellValues.Number)
        {
            return Convert.ToDecimal(cell.CellValue.Text).ToString();
        }
        else
        {
            return cell.CellValue?.Text ?? string.Empty;
        }
    }

    return value.ToString();
}

内容的提问来源于stack exchange,提问作者Lajith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:00:35