/// <summary> /// 表格中查找替换 /// </summary> /// <param name="CurCell"></param> /// <param name="PMember"></param> /// <param name="PResumes"></param> /// <param name="PRelations"></param> /// <param name="PPrizePunishs"></param> /// <param name="PAppraises"></param> private void ReplaceKeyInCell(XWPFDocument WDoc, XWPFTableCell CurCell, Lib.MemberInfoEntity PMember, List <MemberResume> PResumes, List <MemberRelations> PRelations, List <MemberPrizePunish> PPrizePunishs, List <MemberAppraise> PAppraises) { for (int i = 0; i < CurCell.Paragraphs.Count; i++) { XWPFParagraph CurPara = CurCell.Paragraphs[i]; //头像 if (CurPara.ParagraphText.Trim().Contains("[HeadImage]")) { while (CurPara.Runs.Count > 0) { CurPara.RemoveRun(0); } if (_FixedDocVM.HeadImage != null && _FixedDocVM.HeadImage.StreamSource != null) { XWPFRun imageCellRun = CurPara.CreateRun(); //必须将流位置指针设置到开始处,否则显示不出来。 _FixedDocVM.HeadImage.StreamSource.Position = 0; var picID = WDoc.AddPictureData(_FixedDocVM.HeadImage.StreamSource, (int)PictureType.PNG); CreatePictureInLine(imageCellRun, picID, 100, 140); // CreatePictureWithAnchor(imageCellRun, picID, 90, 120); } break; } //简历 if (CurPara.ParagraphText.Trim().Contains("[Resume]")) { if (PResumes != null && PResumes.Count > 0) { for (int pr = 0; pr < PResumes.Count; pr++) { string EndDateStr = PResumes[pr].EndDate.ToString("yyy.MM"); if (PResumes[pr].EndDate.Year == 1) { EndDateStr = "今"; } string tempValue = $"{PResumes[pr].BeginDate:yyyy.MM}--{EndDateStr} {PResumes[pr].Content}"; if (pr == 0) { CurPara.ReplaceText("[Resume]", tempValue); } else { XWPFParagraph NewParagraph = CurCell.AddParagraph(); XWPFRun NewRun = NewParagraph.CreateRun(); NewRun.FontFamily = CurPara.Runs[0].FontFamily; NewRun.FontSize = CurPara.Runs[0].FontSize; NewRun.SetText(tempValue); } } } else { while (CurPara.Runs.Count > 0) { CurPara.RemoveRun(0); } } break; } //奖情惩况 if (CurPara.ParagraphText.Trim().Contains("[PrizePunish]")) { if (PPrizePunishs != null && PPrizePunishs.Count > 0) { for (int pr = 0; pr < PPrizePunishs.Count; pr++) { string tempValue = $"{PPrizePunishs[pr].OccurDate:yyyy.MM} {PPrizePunishs[pr].PrizrOrPunishUnit} {PPrizePunishs[pr].PrizrOrPunishName}"; if (pr == 0) { CurPara.ReplaceText("[PrizePunish]", tempValue); } else { XWPFParagraph NewParagraph = CurCell.AddParagraph(); XWPFRun NewRun = NewParagraph.CreateRun(); NewRun.FontFamily = CurPara.Runs[0].FontFamily; NewRun.FontSize = CurPara.Runs[0].FontSize; NewRun.SetText(tempValue); } } } else { while (CurPara.Runs.Count > 0) { CurPara.RemoveRun(0); } } break; } //近三年考核情况 if (CurPara.ParagraphText.Trim().Contains("[Appraise]")) { if (PAppraises != null && PAppraises.Count > 0) { for (int pr = 0; pr < PAppraises.Count; pr++) { string tempValue = $"{PAppraises[pr].Year:yyyy} {PAppraises[pr].Result}"; if (pr == 0) { CurPara.ReplaceText("[Appraise]", tempValue); } else { XWPFParagraph NewParagraph = CurCell.AddParagraph(); XWPFRun NewRun = NewParagraph.CreateRun(); NewRun.FontFamily = CurPara.Runs[0].FontFamily; NewRun.FontSize = CurPara.Runs[0].FontSize; NewRun.SetText(tempValue); } } } else { while (CurPara.Runs.Count > 0) { CurPara.RemoveRun(0); } } break; } //主要社会关系 if (CurPara.ParagraphText.Trim().Contains("[Relations")) { string tempValue = ""; for (int p = 1; p <= 7; p++) { for (int k = 0; k <= 4; k++) { if (CurPara.ParagraphText.Trim().Equals($"[Relations{p}{k}]")) { if (PRelations != null && PRelations.Count >= p) { switch (k) { case 0: tempValue = $"{PRelations[p - 1].Relation}"; break; case 1: tempValue = $"{PRelations[p - 1].Name}"; break; case 2: tempValue = $"{PRelations[p - 1].Birthday:yyyy.MM}"; break; case 3: tempValue = $"{PRelations[p - 1].PoliticalStatus}"; break; case 4: tempValue = $"{PRelations[p - 1].UnitName} {PRelations[p - 1].Role} {PRelations[p - 1].Remark}"; break; } } CurPara.ReplaceText($"[Relations{p}{k}]", tempValue == null ? "" : tempValue.Trim()); break; } } } } //基本信息 { Type t = PMember.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach (PropertyInfo p in pi) { if (CurPara.ParagraphText.Trim().Contains($"[{p.Name}]")) { string CurValue = p.GetValue(PMember)?.ToString(); if (string.IsNullOrWhiteSpace(CurValue)) { CurValue = ""; } else { DateTime tempDate; switch (p.Name) { case "Birthday": tempDate = DateTime.Parse(CurValue); CurValue = $"{tempDate:yyyy.MM}"; XWPFParagraph NewParagraph = CurCell.AddParagraph(); NewParagraph.Alignment = ParagraphAlignment.CENTER; XWPFRun NewRun = NewParagraph.CreateRun(); NewRun.FontFamily = CurPara.Runs[0].FontFamily; NewRun.FontSize = CurPara.Runs[0].FontSize; NewRun.SetText($"({DateTime.Now.Year - tempDate.Year}岁)"); break; case "JoinCPC": tempDate = DateTime.Parse(CurValue); CurValue = $"{tempDate:yyyy.MM}"; break; case "BeginWork": tempDate = DateTime.Parse(CurValue); CurValue = $"{tempDate:yyyy.MM}"; break; } } CurPara.ReplaceText("[" + p.Name + "]", CurValue); break; } } } } }
private async void BtnSelectFile_Click(object sender, RoutedEventArgs e) { Button CurBtn = sender as Button; int AddCount = 0; // 在WPF中, OpenFileDialog位于Microsoft.Win32名称空间 Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog { Filter = "Excel Files|*.xls;*.xlsx" }; if (dialog.ShowDialog() == true) { CurBtn.IsEnabled = false; try { System.IO.FileInfo theExcelFile = new System.IO.FileInfo(dialog.FileName); OperationMsg = $"正在从文件[{theExcelFile.FullName}]中导入人员信息,请稍候...."; await Task.Run(async() => { DataTable UserTable = NpoiExcel.ReadStreamToDataTable(theExcelFile.OpenRead(), null, true); if (UserTable != null && UserTable.Rows.Count > 0) { Lib.MemberInfoEntity tempMember = new Lib.MemberInfoEntity(); PropertyInfo[] Attri = tempMember.GetType().GetProperties(); for (int i = 0; i < UserTable.Rows.Count; i++) { tempMember = new Lib.MemberInfoEntity(); foreach (PropertyInfo item in Attri) { //获取该属性的描述特性值(比如:[Description("身份证号")]中的 "身份证号" ) object[] objs = item.GetCustomAttributes(typeof(DescriptionAttribute), true); string PropDescription = ((DescriptionAttribute)objs[0]).Description; if (UserTable.Columns.Contains(PropDescription) && UserTable.Rows[i][PropDescription] != null) { StringBuilder dateStr = new StringBuilder(UserTable.Rows[i][PropDescription].ToString()); switch (item.PropertyType.FullName) { case "System.DateTime": if (!DateTime.TryParse(dateStr.ToString(), out DateTime tempDateTime)) { //处理Excel中数据不规范情况。 dateStr = dateStr.Replace('.', '/'); dateStr = dateStr.Replace('-', '/'); dateStr = dateStr.Replace('—', '/'); dateStr = dateStr.Replace('。', '/'); if (dateStr.Length < 8) { dateStr.Append("/01"); } } if (DateTime.TryParse(dateStr.ToString(), out tempDateTime)) { item.SetValue(tempMember, tempDateTime); } break; case "System.Int32": if (int.TryParse(dateStr.ToString(), out int tempIntValue)) { item.SetValue(tempMember, tempIntValue); } break; case "System.String": item.SetValue(tempMember, UserTable.Rows[i][PropDescription].ToString()); break; default: continue; } } } if (!string.IsNullOrEmpty(tempMember.Id)) { tempMember.UserId = AppSet.LoginUser.Id; ExcuteResult excuteResult = await DataMemberRepository.AddOrUpdate(tempMember).ConfigureAwait(false); if (excuteResult.State == 0) { AddCount++; OperationMsg = $"正在从文件[{theExcelFile.FullName}]中导入人员信息,已导入[{AddCount}]条信息。"; } } } } });//Task Finish OperationMsg = $"人员信息导入成功,共导入[{AddCount}]条信息。"; CurBtn.IsEnabled = true; } catch (Exception VE) { AppFuns.ShowMessage("出现错误:" + VE.Message, Caption: "错误", isErr: true); CurBtn.IsEnabled = true; return; } } }