0

I am reading Excel file by using OLEDBConnection in C#.

I want to change current thread culture as per culture of Excel file so I can get dates in culture specific formats. I have used following code but it is not working. It still converts date to us English format. If I change Format from Control Panel --> Region and Language then it works:

See screen shot

My Code:

public static System.Data.DataTable GetWorksheet(string worksheetName, string connectionString)
{
   try
   {
      Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ", true);
      Thread.CurrentThread.CurrentCulture.ClearCachedData();
      OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
      OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter(
      //"select * from [" + worksheetName + "]", con);
      "select * from [" + worksheetName + "$]", con);

      con.Open();
      System.Data.DataSet excelDataSet = new DataSet();
      cmd.Fill(excelDataSet);
      con.Close();
      con.Dispose();
      System.Data.DataTable dt = excelDataSet.Tables[0];
      if (excelDataSet != null)
         excelDataSet.Dispose();

      return dt;
   }
   catch (Exception e)
   {
      throw e;
   }
}
Brian
  • 5,069
  • 7
  • 37
  • 47
user2671548
  • 1
  • 1
  • 2
  • 4
  • `Thread.CurrentThread.CurrentCulture.ClearCachedData();` looks suspicious. Try to comment that out – T.S. Sep 26 '13 at 22:18
  • Looks like your problem is `Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ", true);` Use overload `Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ");` because Boolean suggests that you should use culture from user settings on your machine... I believe – T.S. Sep 26 '13 at 22:22

1 Answers1

0

Check out the examples here from Microsoft. They use:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
Vulcronos
  • 3,428
  • 3
  • 16
  • 24