【问题标题】:How to change date format culture for c# application which reads Excel如何更改读取 Excel 的 c# 应用程序的日期格式文化
【发布时间】:2013-09-26 22:00:31
【问题描述】:

我正在使用 C# 中的 OLEDBConnection 读取 Excel 文件。

我想根据 Excel 文件的文化更改当前线程文化,以便我可以获取文化特定格式的日期。我使用了以下代码,但它不起作用。它仍然将日期转换为我们的英文格式。如果我将格式从Control Panel --> Region and Language 更改为有效:

我的代码:

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

【问题讨论】:

  • Thread.CurrentThread.CurrentCulture.ClearCachedData(); 看起来很可疑。试着评论一下
  • 看起来你的问题是Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ", true); 使用重载Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ"); 因为布尔值建议你应该使用机器上用户设置中的文化......我相信

标签: c# excel locale cultureinfo culture


【解决方案1】:

查看来自 Microsoft 的示例 here。他们使用:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");

【讨论】:

  • CurrentUICulture 用于资源管理器,在这种情况下不需要
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2010-09-29
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多