locked
Problem in reading Excel data RRS feed

  • Question

  • I am having Exce data as below,

    ID       Name        Value

    1256  aaaaa        wekadfa

    1254 bbbbb             100.50

    I am having two type of values(string and Integer) in a single column. How to read the different values from a single column....

    help me about this...

    Thanks in advance..

    Uvanesh.

    Some one

     

    • Moved by Leo Liu - MSFT Thursday, June 16, 2011 5:30 AM Moved for better support. (From:Visual C# General)
    Tuesday, June 14, 2011 7:21 PM

Answers

  • Hi Uvanesh

     

    Thanks for your post.

     

    You should know something about Excel data type fist:

    http://msdn.microsoft.com/en-us/library/ms712640(v=VS.85).aspx

     

    When Excel accepts some data, it will decide the best appropriate type to show the data.

    Particularly, when you input something like “123”, Excel will convert it into double type rather than integer.

     

    Here is sample code to detect the type of Excel:

              foreach (Excel.Range r in rng)

                {

                    var value = r.Value;

                    if (ValueType.ReferenceEquals(value.GetType(), typeof(double)))

                    {

                        double doubleValue = Convert.ToDouble(r.Value);

                    }

                    else if (ValueType.ReferenceEquals(value.GetType(), typeof(DateTime)))

                    {

                        DateTime dateValue = Convert.ToDateTime(r.Value);

                    }

                    else if (ValueType.ReferenceEquals(value.GetType(), typeof(decimal)))

                    {

                        decimal deValue = Convert.ToDecimal(r.Value);

                    }

                    else

                    {

                        stringValue = Convert.ToString(r.Value);

                    }

                }

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Calvin_Gao Wednesday, June 29, 2011 12:02 PM
    Tuesday, June 21, 2011 9:44 AM

All replies

  • To start with, this should probably be in the Excel developer's forum and not in C#.

    Either way, what problem exactly are you having? The Range.Value function returns an Object, which you can then convert to whatever data type it really is.

    Tuesday, June 14, 2011 7:50 PM
  • Hi Uvanesh

     

    Thanks for your post.

     

    You should know something about Excel data type fist:

    http://msdn.microsoft.com/en-us/library/ms712640(v=VS.85).aspx

     

    When Excel accepts some data, it will decide the best appropriate type to show the data.

    Particularly, when you input something like “123”, Excel will convert it into double type rather than integer.

     

    Here is sample code to detect the type of Excel:

              foreach (Excel.Range r in rng)

                {

                    var value = r.Value;

                    if (ValueType.ReferenceEquals(value.GetType(), typeof(double)))

                    {

                        double doubleValue = Convert.ToDouble(r.Value);

                    }

                    else if (ValueType.ReferenceEquals(value.GetType(), typeof(DateTime)))

                    {

                        DateTime dateValue = Convert.ToDateTime(r.Value);

                    }

                    else if (ValueType.ReferenceEquals(value.GetType(), typeof(decimal)))

                    {

                        decimal deValue = Convert.ToDecimal(r.Value);

                    }

                    else

                    {

                        stringValue = Convert.ToString(r.Value);

                    }

                }

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Calvin_Gao Wednesday, June 29, 2011 12:02 PM
    Tuesday, June 21, 2011 9:44 AM