C# OleDbDataAdapter. Date format when reading Excel file

563 Views Asked by At

I have connection string with IMEX option (all types are converted to string):

strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"{0}\";Extended Properties=\"Excel 12.0 Xml;HDR=NO;IMEX=1;\";", strFile);

I use followed code to read xslx file into DataTable

DataTable table = new DataTable(name);
string sql = "SELECT * FROM [" + name + "]";
OleDbDataAdapter SQLAdapter = new OleDbDataAdapter();
OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn);
SQLAdapter.SelectCommand = selectCMD;
SQLAdapter.Fill(table);

Program reads the same file on two computers.

When the program runs on the first computer, I get date like string "31.03.2021". When the program runs on the second computer, I get the date like string "3/31/2021".

I cannot get the same date format on both computers. I already tried to:

  1. Change the language format in Windows settings.

  2. Set

     Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
  3. Set

     table.Locale = new CultureInfo("en-US");
    

Nothing helps

1

There are 1 best solutions below

0
Igor Semkiv On

I couldn't find any Solution. So I started to use a option IMEX=0 instead of IMEX=1. Date are read correctly as DateTime type.