Excel标头是合并的单元格

时间:2019-01-25 06:25:02

标签: c# excel wpf xaml wpfdatagrid

我当前正在创建一个考勤应用程序,该应用程序将从excel工作表和日志文件计算其工作时间。

我正在将Excel文件上传到dataGrid。

这是excel文件的外观 Excel File

这是当我尝试将其放在DataGrid上时的输出 DataGrid Output

我还尝试将HDR设置为“否”,这就是发生的情况 No Header

这是我当前的XAML代码

<DataGrid
       x:Name="dgvAttendance"
       Margin="0 5 0 0"
       CanUserSortColumns="True"
       CanUserAddRows="False" HorizontalScrollBarVisibility="Visible" SelectedIndex="0" >
       </DataGrid>

这是我的代码,用于填充我的DataGrid

OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
                openFileDialog1.Filter = "XML Files (*.xml; *.xls; *.xlsx; *.xlsm; *.xlsb) |*.xml; *.xls; *.xlsx; *.xlsm; *.xlsb";
                openFileDialog1.FilterIndex = 3;

                openFileDialog1.Multiselect = false;  
                openFileDialog1.Title = "Open Text File-R13";  
                openFileDialog1.InitialDirectory = @"Desktop";

                openFileDialog1.ShowDialog();
                string pathName = openFileDialog1.FileName;
                string fileName = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                DataTable tbContainer = new DataTable();
                string strConn = string.Empty;
                string sheetName = "2018";

                FileInfo file = new FileInfo(pathName);
                if (!file.Exists) { throw new Exception("Error, file doesn't exists!"); }
                string extension = file.Extension;
                switch (extension)
                {
                    case ".xls":
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1;'";
                            break;
                    case ".xlsx":
                        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1;'";
                            break;
                    default:
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1;'";
                            break;
                }

                OleDbConnection cnnxls = new OleDbConnection(strConn);
                OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), cnnxls);
                oda.Fill(tbContainer);
                dgvAttendance.ItemsSource = tbContainer.DefaultView;

我希望输出看起来像合并的而不是不损坏的Excel标头,而不是打破了DataGrid的当前输出

0 个答案:

没有答案