【发布时间】: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