Is it possible to ignore Excel warnings when generating spreadsheets using EPPlus?

前端 未结 4 411
余生分开走
余生分开走 2020-12-06 18:54

I\'m storing mix of numeric and non-numeric values in a single column in spreadsheet using C# and EPPlus. When I open spreadsheet with Excel it shows green triangles in the

4条回答
  •  一向
    一向 (楼主)
    2020-12-06 19:11

    This is a derivation of TechnoPriest's answer that works for me - I've added handling of decimal values, and changed the name of the method to more accurately document its true raison d'etre:

    public static void ConvertValueToAppropriateTypeAndAssign(this ExcelRangeBase range, object value)
    {
        string strVal = value.ToString();
        if (!String.IsNullOrEmpty(strVal))
        {
            decimal decVal;
            double dVal;
            int iVal;
    
            if (decimal.TryParse(strVal, out decVal))
            {
                range.Value = decVal;
            }
            else if (double.TryParse(strVal, out dVal))
            {
                range.Value = dVal;
            }
            else if (Int32.TryParse(strVal, out iVal))
            {
                range.Value = iVal;
            }
            else
            {
                range.Value = strVal;
            }
        }
        else
        {
            range.Value = null;
        }
    }
    

提交回复
热议问题