/// <summary> /// XLS导入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnImportEqu_Click(object sender, RoutedEventArgs e) { SecurityClass[] securityclasses = CO_IA.Client.Utility.GetSecurityClasses(); if (securityclasses == null || securityclasses.Length == 0) { MessageBox.Show("请先在基础数据设置中增加保障类别"); } else { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Excel文件(*.xls)|*.xls"; //dialog.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls"; //dialog.DefaultExt = "xlsx"; dialog.CheckFileExists = true; if (dialog.ShowDialog() == true) { DataTable[] tables = ExcelImportHelper.LoadDataFromExcel(dialog.FileName); if (tables != null && tables.Length > 0) { //单位信息 Organization orginfo = null; DataTable orgtable = tables.FirstOrDefault(r => r.TableName == "单位信息"); if (orgtable != null && ExcelImportHelper.ValidateORG(orgtable)) { orginfo = this.LoadORGFromTable(orgtable); } if (orginfo != null) { #region 验证单位名称 OrgQueryCondition condition = new OrgQueryCondition(); condition.Name = orginfo.Name; Organization[] orgs = this.GetORGSource(condition); Organization sameorg = orgs.FirstOrDefault(r => r.Name == orginfo.Name); #endregion if (sameorg != null) { MessageBoxResult result = MessageBox.Show(string.Format("'{0}'已经存在,是否将Excel中的设备导入到'{0}'中?", orginfo.Name), "提示", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { ImportEquipment(tables, sameorg); } else { MessageBox.Show("请修改单位名称"); } } else { ImportEquipment(tables, orginfo); } } } } } }
public static void Import(Action <ActivityOrganization, List <ActivityEquipment> > p_importEquips) { ImportEquipmentDelegate = p_importEquips; OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Excel文件(*.xls)|*.xls"; dialog.DefaultExt = "xls"; dialog.CheckFileExists = true; if (dialog.ShowDialog() == true) { DataTable[] tables = ExcelImportHelper.LoadDataFromExcel(dialog.FileName); if (tables != null && tables.Length > 0) { //单位信息 ActivityOrganization activityorg = null; DataTable orgtable = tables.FirstOrDefault(r => r.TableName == "单位信息"); if (orgtable != null && ExcelImportHelper.ValidateORG(orgtable)) { activityorg = LoadActivityORGFromTable(orgtable, SystemLoginService.CurrentActivity.Guid); } if (activityorg != null) { #region 验证单位名称 OrgQueryCondition condition = new OrgQueryCondition(); condition.ActivityGuid = SystemLoginService.CurrentActivity.Guid; condition.Name = activityorg.Name; ActivityOrganization[] orgs = DataOperator.GetActivityOrgSources(condition); ActivityOrganization sameorg = orgs.FirstOrDefault(r => r.Name == activityorg.Name); #endregion if (sameorg != null) { MessageBoxResult result = MessageBox.Show(string.Format("单位'{0}'已经存在,是否将Excel中的设备导入到现有的单位中?", activityorg.Name), "提示", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { ImportActivityEquipment(tables, sameorg); } else { MessageBox.Show("请修改单位名称"); } } else { ImportActivityEquipment(tables, activityorg); } } } } }
/// <summary> /// 导入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnXLSImport_Click(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Excel 文件|*.xls;*.xlsx"; dialog.DefaultExt = "xls"; dialog.CheckFileExists = true; if (dialog.ShowDialog() == true) { DataTable[] tables = ExcelImportHelper.LoadDataFromExcel(dialog.FileName); if (tables != null && tables.Length > 0) { string imgPath = dialog.FileName.Substring(0, dialog.FileName.LastIndexOf('\\') + 1) + "车辆照片"; if (Directory.Exists(imgPath)) { List <DataRow> rows = VehicleImportHelper.GetEnableDataRow(tables); if (rows != null && rows.Count > 0) { VehicleImportHelper.ImageFilePath = imgPath; List <VehicleInfo> vehicles = null; bool result = false; vehicles = VehicleImportHelper.GetVehicleFromTable(rows, out result); //设备获取成功 if (result) { if (vehicles != null && vehicles.Count > 0) { List <VehicleInfo> samenolst = new List <VehicleInfo>(); //验证在数据库中是否存在相同的车辆编号 if (VehicleImportHelper.VerifyVehicleNotInDB(vehicles, out samenolst)) { try { BeOperationInvoker.Invoke <I_CO_IA_PlanDatabase> (channel => { channel.ImportVehicles(vehicles); }); GetVehicleInfos(); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMessage(), "导入失败"); } } else { StringBuilder errstr = new StringBuilder(); errstr.AppendLine("数据库存在相同的车牌号码:"); foreach (VehicleInfo item in samenolst) { errstr.AppendLine(item.VehicleNo); } errstr.AppendLine("是否进行替换?"); errstr.AppendLine("说明:如果车辆在监测实施中,存在设备。则不替换'监测车'属性"); MessageBoxResult msgresult = MessageBox.Show(errstr.ToString(), "提示", MessageBoxButton.YesNo); if (msgresult == MessageBoxResult.Yes) { BeOperationInvoker.Invoke <I_CO_IA_PlanDatabase> (channel => { channel.ImportVehicles(vehicles); }); GetVehicleInfos(); } else { return; } } } } } else { MessageBox.Show("车辆信息为空,请填写车辆信息", "提示", MessageBoxButton.OK); } } else { MessageBox.Show("车辆照片文件夹不存在,无法导入照片"); } } else { MessageBox.Show("车辆信息为空,请填写车辆信息", "提示", MessageBoxButton.OK); } } }