在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”。我认为这是问题的根源。怎么解决呢?     
已邀请:
        如果在Excel中实际是6-1-2011(不是2001,是错字?),则40695表示OLE日期。因此,您可以通过以下方式进行转换:
DateTime dt = DateTime.FromOADate(40695);
对应于6/1/2011。 在IValueConverter中使用上面的代码,以获取正确的值以显示在DataGrid中。     

要回复问题请先登录注册