Пример #1
0
        public static void CellValueToStr(Interop.Range cell, int tblColIdx, out string cellValToObj)
        {
            string typeName;

            if (cell.Value == null)
            {
                typeName = "null";
            }
            else
            {
                typeName = cell.Value.GetType().ToString().Replace("System.", "").ToLower();
            }

            switch (typeName)
            {
            case "string":
                cellValToObj = Convert.ToString(cell.Value2);
                break;

            case "double":
                cellValToObj = Convert.ToString(
                    NumberCellsHandler.ConvertToDecimalDataType(cell.Value2)
                    );
                break;

            case "boolean":
                try
                {
                    cellValToObj = Convert.ToString(Convert.ToBoolean(cell.Value2)).ToUpper();
                }
                catch (FormatException ex)
                {
                    throw new FormatException(
                              $"The {cell.Value2.GetType().Name} value {Convert.ToString(cell.Value2)} is not recognized as a valid boolean value."
                              );
                }
                catch (InvalidCastException ex)
                {
                    throw new InvalidCastException(
                              $"Conversion of the {cell.Value.GetType().Name} value {Convert.ToString(cell.Value)} to a boolean value is not supported."
                              );
                }

                break;

            case "datetime":
                cellValToObj = Convert.ToString(DateTimeCellsHandler.CovertCellValuesToDateTime(cell.Value2));
                break;

            case "null":
                cellValToObj = String.Empty;
                break;

            default:
                throw new Exception($"Application can't convert {typeName} table cells to their string representation.");
            }
        }