public static FileStream GenerateMilitaryComissariatReport(List <Recruit> recruits, MilitaryComissariat militaryComissariat) { var index = 1; var recruitsList = recruits.Select(m => new { Index = index++, m.LastName, m.FirstName, m.Patronymic, m.BirthDate }).ToList(); const string templateFile = TemplatePath + "Dactyloscopy/comissariat_report.xlsx"; const string tempFile = TempPath + "comissariat_report.xlsx"; CopyTemplateFileToTempDirectory(templateFile, tempFile); var document = new XLTemplate(tempFile); document.AddVariable("Number", militaryComissariat.InnerCode.TrimStart('0')); var firstRecruitDeliveryDate = recruits.First().DeliveryDate; document.AddVariable("Season", (firstRecruitDeliveryDate.Month > 9 ? "осенью" : "весной") + " " + firstRecruitDeliveryDate.Year); document.AddVariable("Comissariat", militaryComissariat.GetDocumentName()); document.AddVariable("Recruits", recruitsList); document.Generate(); document.SaveAs(tempFile); return(new FileStream(tempFile, FileMode.Open)); }
public static FileStream GeneratePersonalGuidanceReport(List <SpecialPerson> persons, MilitaryComissariat militaryComissariat) { const string templateFile = TemplatePath + "PersonalGuidance/person_report.docx"; var tempFile = TempPath + $"PersonalGuidance/{militaryComissariat.Name}.docx"; if (!Directory.Exists(TempPath + "PersonalGuidance")) { Directory.CreateDirectory(TempPath + "PersonalGuidance"); } CopyTemplateFileToTempDirectory(templateFile, tempFile); var replaceTextDictionary = new Dictionary <string, string> { { "MilitaryComissariat", militaryComissariat.Name }, }; using (var document = WordprocessingDocument.Open(tempFile, true)) { try { var bookMarks = FindBookmarks(document.MainDocumentPart.Document); RemoveBookMarkContent(bookMarks); var runFonts = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman" }; var fontSize = new FontSize() { Val = "28" }; var fontSizeComplexScript = new FontSizeComplexScript() { Val = "28" }; var runProperties = new RunProperties(new Bold(), runFonts, fontSize, fontSizeComplexScript); foreach (var(bookmarkStart, bookmarkEnd) in bookMarks) { if (!replaceTextDictionary.Select(m => m.Key).Contains <string>(bookmarkStart.Name)) { continue; } var replaceText = replaceTextDictionary.First(m => m.Key == bookmarkStart.Name).Value; var run = new Run(); var text = new Text(replaceText); run.Append(runProperties.CloneNode(true), text); bookmarkEnd.InsertAfterSelf(run); } var body = document.MainDocumentPart.Document.Body; var index = 1; var runFontsTable = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman" }; var fontSizeTable = new FontSize() { Val = "24" }; var fontSizeComplexScriptTable = new FontSizeComplexScript() { Val = "24" }; var runPropertiesTable = new RunProperties(runFontsTable, fontSizeTable, fontSizeComplexScriptTable); var paragraphProperties = new ParagraphProperties(new SpacingBetweenLines() { After = "0" }); foreach (var table in body.Descendants <Table>()) { foreach (var person in persons) { var tableCellIndex = new TableCell(new Paragraph(paragraphProperties.CloneNode(true), new Run(runPropertiesTable.CloneNode(true), new Text(index.ToString())))); var tableCellFullName = new TableCell(new Paragraph(paragraphProperties.CloneNode(true), new Run(runPropertiesTable.CloneNode(true), new Text(person.FullName)))); var tableCellBirthYear = new TableCell(new Paragraph(paragraphProperties.CloneNode(true), new Run(runPropertiesTable.CloneNode(true), new Text(person.BirthYear.ToString())))); var tableCellMilitaryUnitCode = new TableCell(new Paragraph( paragraphProperties.CloneNode(true), new Run(runPropertiesTable.CloneNode(true), new Text(person.Requirement.MilitaryUnitCode)))); var tableCellMilitaryUnitName = new TableCell(new Paragraph( paragraphProperties.CloneNode(true), new Run(runPropertiesTable.CloneNode(true), new Text(person.Requirement.MilitaryUnit.Name)))); var tableCellSendDate = new TableCell(new Paragraph(paragraphProperties.CloneNode(true), new Run(runPropertiesTable.CloneNode(true), new Text(person.Notice)))); var tableRow = new TableRow(); tableRow.Append(tableCellIndex, tableCellFullName, tableCellBirthYear, tableCellMilitaryUnitCode, tableCellMilitaryUnitName, tableCellSendDate); table.AppendChild(tableRow); index++; } } document.MainDocumentPart.Document.Save(); } catch { document.MainDocumentPart.Document.Save(); throw new Exception("Неизвестная ошибка"); } } return(new FileStream(tempFile, FileMode.Open)); }