DataGrid 不显示来自 Excel 的数据

本文关键字:Excel 数据 显示 DataGrid | 更新日期: 2023-09-27 18:33:22

我的 WPF 应用程序中的数据网格不显示 Excel 工作表中的数据。它只显示水平线。

private void btUpload_Click(object sender, RoutedEventArgs e)
    {
        // Configure open file dialog box 
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        // Filter by Excel Worksheets
        dlg.Filter = "Excel Worksheets|*.xls";
        dlg.ShowDialog();
        // Show open file dialog box 
        Nullable<bool> result = dlg.ShowDialog();
        //string FileName = "dlg"; // From the dialog box.
   // Process open file dialog box results 
       if (result == true)
        {
           string path = System.IO.Path.GetFullPath("dlg");
            string query = "SELECT * FROM [code$]";
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dlg.FileName + ";Extended Properties=" + "'"Excel 12.0 Xml;HDR=YES;IMEX=1'"";
            OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
            //DataSet dataSet = new DataSet();
            DataSet dsCodes = new DataSet();
            adapter.Fill(dsCodes);
        dgCodeDisp.ItemsSource = dsCodes.DefaultViewManager;
}

DataGrid xaml 代码:

<DataGrid x:Name="dgCodeDisp" Grid.Column="1" HorizontalAlignment="Left" Height="141" Margin="260,230,0,0" VerticalAlignment="Top" Width="240" SelectionChanged="DataGrid_SelectionChanged" Grid.ColumnSpan="3" Grid.RowSpan="2" />

DataGrid 不显示来自 Excel 的数据

数据网格的自动生成列设置为 true