I use this function to read information from xls (2003) file
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", path);
var adapter = new OleDbDataAdapter("SELECT * FROM [page1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
var data = ds.Tables["anyNameHere"].AsEnumerable();
List<client> query = data.Where(x => x.Field<string>(1) != string.Empty)
.Select(x =>
new client
{
Code = x.Field<string>(1) != null ? x.Field<string>(1) : "None",
name = x.Field<string>(2),
}
some code in file have Int format like “2100” and other like “G200” but the field(1) was read like double and can’t read second type (cast error). I took a look to xls file and the column in standard type even I make it text but I have the same problem.
PS: I used step by step and I found that the data have the field code for the second type NULL.
@GertArnold :
1=> M ; 2801 ; Ronald
2=> M ; 3005 ; Soufian
3=> F ; 2009 ; elizabeth
.
.
121 => M ; G125 ; khaled => le code est lu null
I make a 4th column with formatting => =TEXTE(B1;”####”) and make the same formatting for all the colmne cell and I get the code from this column and it work