/// <summary> /// 导入到主页面中的DataGrid展示表格中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DaoRu_Click(object sender, RoutedEventArgs e) { //导入Excel数据预览,这块只是将Excel中的数据加载出来之后绑定到DATAGRID控件上 //1.这是打开文件的一个弹框 Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog(); //2.默认的文件后缀名类型 fileDialog.Filter = "工作簿Excel文件(*.xlsx;*.xls)|*.xlsx;*.xls"; //3.打开文件 if (fileDialog.ShowDialog() == true) { //4.获取文件路径 string path = fileDialog.FileName; //5.调用业务逻辑中学生:找到对应的方法获取从Excel中拿到所有的数据----》在找访问层把所有的在Excel库中学生的数据用list范性全部拿到----》在学生中找对应的属性赋值 list = manager.GetStudentByExcel(path); //***先把DataGrid(名字时dgStudent)标签里面的东西全部清空 dgStudent.ItemsSource = null; dgStudent.AutoGenerateColumns = false; //从新赋值给DataGrid(名字时dgStudent)标签 dgStudent.ItemsSource = list; } }