OleDB? I don't know if you can tell it that every thing is string, I
actually doubt it, I'm not telling for sure because of lack of
experience with it over excel... then again, that's because I use
excel's libs to access the information of excel (I think that doing
that makes sence), and there I haven't had this problems. By the way,
you can always use toString on whatever you read, and using
InvariantCulture even better. Excel isn't too good for punctuation
(office isn't any way), you know that "." vs "," thing. I remember
doing somehting like the code you present for access, there I had to
write a not too pretty if set (hard to become into a switch), to deal
with this kind of situations. I want to add... that that sucks. You
are know facing a double... wait till you hit a date. -_-


On 4 mayo, 13:49, comicrage <[email protected]> wrote:
> Hi,
>
> I am parsing an excel spreadsheet to read in all the fields and one of
> the string values is '01'. When the following line
>
> object val = reader[col.GetSelectColumn()];
>
> retrieves the value, it parse and reads the '01' to 1.0, which is a
> double type. When the .SetValue line is executed, an error occurs
> citing Double type cannot be asigned to a string, which is string, I
> want to assign the value. My question: How can I get the
> OleDbDataReader reader to read every value as they are in the
> spreadsheet, basically, read them all as string?
>
> Any help is greatly appreciated... Thanks.
>
> using the like the following
>
>  string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
> Source=" + Filepath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
>                 List<ExcelColumnAttribute> columns = GetColumnList();
>                 rows.Clear();
>                 using (OleDbConnection conn = new
> OleDbConnection(connectionString))
>                 {
>                     conn.Open();
>                     using (OleDbCommand cmd = conn.CreateCommand())
>                     {
>                         cmd.CommandText = "SELECT * FROM [SHEET1$]";
>                         using (OleDbDataReader reader =
> cmd.ExecuteReader())
>                         {
>                             while (reader.Read())
>                             {
>                                 T item = CreateInstance();
>                                 foreach (ExcelColumnAttribute col in
> columns)
>                                 {
>                                     object val =
> reader[col.GetSelectColumn()];
>                                     if (col.IsFieldStorage())
>                                     {
>                                         FieldInfo fi =
> typeof(T).GetField(col.GetStorageName(), BindingFlags.GetField |
> BindingFlags.NonPublic | BindingFlags.Instance |
> BindingFlags.SetField);
>                                         if (val != null && val !=
> DBNull.Value)
>                                         {
>                                             fi.SetValue(item, val);
>                                         }
>                                     }
>                                     else
>                                     {
>
> typeof(T).GetProperty(col.GetStorageName()).SetValue(item, val, null);
>                                     }
>                                 }
>                                 rows.Add(item);
>                             }
>                         }
>                     }
>                 }

Reply via email to