Search code examples
c#.netopenxmldocumentformat

Issue Switch statement after upgrading


I Have upgraded DocumentFormat.OpenXml to 3.0,0. After upgrading my excel reading function throws compile time error.

This is Fuction to get Cell values

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

Error i Am getting on Switch function.

a constant value of type cellvalues is expected

enter image description here


Solution

  • You cannot use a switch statement for readonly record struct. According to the C# 8 documentation:

    If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, bool, string, or an enum_type, or if it is the nullable value type corresponding to one of these types, then that is the governing type of the switch statement.

    You will need to refactor your code to use if/else:

    if (cell.DataType.Value == CellValues.SharedString)
    {
        return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(cell.CellValue.Text)].InnerText;
    }
    else
    {
        ...
        ...