在DataGrid中查看Excel日期单元格内容时遇到问题
|
我正在将Excel工作表加载到WPF中的“ 0”控件。
在Excel工作表的单元格中,我有一个值为1.6.2011的日期。
如果我使用此方法加载此Excel文件:
private static DataSet LoadExcelFile(string file,string sheetName)
{
string connString= \"Provider=Microsoft.Jet.OLEDB.4.0;\" +
\"Data Source=\" + file + \";\" +
\"Extended Properties=Excel 8.0;\";
var oledbConn = new OleDbConnection(connString);
try
{
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet Sheet1
var cmd = new OleDbCommand(\"SELECT * FROM [\"+sheetName+\"$]\", oledbConn);
// Create new OleDbDataAdapter
var oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
// Create a DataSet which will hold the data extracted from the worksheet.
var ds = new DataSet();
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds, \"Employees\");
return ds;
}
catch
{
throw new Exception();
}
finally
{
// Close connection
oledbConn.Close();
}
}
然后将DataSet
添加到DataGrid
中:
Dps.ItemsSource = ExcelFile.Tables[0].DefaultView;
那么问题是Excel工作表中的值是1-6-2001,但是在WPF中,值“ 0”被视为“ 6”。
我做错了什么?我认为这是由Excel中的单元格格式引起的。 Excel中单元格的数据类型为“ 7”。我认为这是问题的根源。怎么解决呢?
没有找到相关结果
已邀请:
1 个回复
室邢
对应于6/1/2011。 在IValueConverter中使用上面的代码,以获取正确的值以显示在DataGrid中。