/// <summary> /// 模型数据改变 /// </summary> /// <param name="obj"></param> private void dataChanged(EntDepartmentInfoModel enterpriseModel) { getPageData(pageRepuestParams.PageIndex, pageRepuestParams.PageSize); var tmpModel = EntDepartmentInfoList.FirstOrDefault(a => a.Id == enterpriseModel.Id); this.EntDepartmentInfo = EntDepartmentInfoList.FirstOrDefault(); }
/// <summary> /// 执行导出命令 /// </summary> private async void OnExecuteExportCommand() { var window = (MetroWindow)Application.Current.MainWindow; var dialogResult = await window.ShowMessageAsync("数据导出" , "确定要从磁盘中导出企业信息数据吗?" , MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "是", NegativeButtonText = "否" }); if (dialogResult == MessageDialogResult.Affirmative) { string filePath = ""; System.Windows.Forms.SaveFileDialog openFileDialog = new System.Windows.Forms.SaveFileDialog(); openFileDialog.FileName = "企业信息" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".xls"; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意,此处一定要手动引入System.Window.Forms空间,否则你如果使用默认的DialogResult会发现没有OK属性 { filePath = openFileDialog.FileName; } if (string.IsNullOrEmpty(filePath)) { Application.Current.Resources["UiMessage"] = "数据导出失败,选择存储路径为空!"; return; } if (EntDepartmentInfoList.Any()) { try { EntDepartmentInfoModel model = new EntDepartmentInfoModel(); List <string> displayNameList = model.GetDisplayName(); DataTable dataTable = EntDepartmentInfoList.ToDataTable <EntDepartmentInfoModel>(displayNameList); //filePath += @"\企业信息" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".xls"; NPOIHelper.Export(dataTable, "企业信息", filePath); } catch (Exception ex) { Application.Current.Resources["UiMessage"] = "数据导出失败!" + ex.ToString(); } if (File.Exists(filePath)) { Application.Current.Resources["UiMessage"] = "数据导出已完成!"; } else { Application.Current.Resources["UiMessage"] = "数据导出失败!"; } } else { Application.Current.Resources["UiMessage"] = "数据导出失败,无数据!"; } } }
/// <summary> /// 执行导入命令 /// </summary> private async void OnExecuteImportCommand() { var window = (MetroWindow)Application.Current.MainWindow; var dialogResult = await window.ShowMessageAsync("数据导入" , "确定要导入企业信息数据到磁盘中吗?将会删除磁盘中原有数据" , MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "是", NegativeButtonText = "否" }); if (dialogResult == MessageDialogResult.Affirmative) { string filePath = ""; System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意,此处一定要手动引入System.Window.Forms空间,否则你如果使用默认的DialogResult会发现没有OK属性 { filePath = openFileDialog.FileName; } if (string.IsNullOrEmpty(filePath)) { Application.Current.Resources["UiMessage"] = "数据导入失败,选择存储路径为空!"; return; } if (EntDepartmentInfoList.Any()) { try { EntDepartmentInfoModel model = new EntDepartmentInfoModel(); Dictionary <String, Type> filedNameDc = model.GetFiledNameAndType(); DataTable dt = NPOIHelper.Import(filePath); DataTable dtNew = new DataTable(); foreach (var kvp in filedNameDc) { dtNew.Columns.Add(kvp.Key, kvp.Value); } object[] values = new object[filedNameDc.Count]; foreach (DataRow row in dt.Rows) { for (int i = 0; i < dtNew.Columns.Count; i++) { values[i] = row[i]; } dtNew.Rows.Add(values); } List <EntDepartmentInfoModel> enterpriseModels = dtNew.DataTableToList <EntDepartmentInfoModel>(); } catch (Exception ex) { Application.Current.Resources["UiMessage"] = "数据导入失败!" + ex.ToString(); } if (File.Exists(filePath)) { Application.Current.Resources["UiMessage"] = "数据导入已完成!"; } else { Application.Current.Resources["UiMessage"] = "数据导入失败!"; } } else { Application.Current.Resources["UiMessage"] = "数据导入失败,无数据!"; } } }