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