DepartmentProject/DepartmentPortal/AcademicProgressBusinessLogic/BusinessLogic/SaveToWord.cs

266 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AcademicProgressBusinessLogic.HelperModels;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AcademicProgressBusinessLogic.BusinessLogic
{
public class SaveToWord
{
public static void CreateDoc(WordInfo info)
{
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(info.FileName + "//" + info.Title + ".docx", WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body docBody = mainPart.Document.AppendChild(new Body());
docBody.AppendChild(CreateParagraph(new WordParagraph
{
Texts = new List<string> { info.Title },
TextProperties = new WordParagraphProperties
{
Bold = true,
Size = "24",
JustificationValues = JustificationValues.Center
}
}));
if (info.StudentScores != null)
{
foreach (var rp in info.StudentScores)
{
docBody.AppendChild(CreateParagraph(new WordParagraph
{
Texts = new List<string> { "Приложение к диплому студента - " + rp.StudentLastName + rp.StudentFirstName },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
break;
}
Table table = new Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
new LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
new RightBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 },
new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 2 }
));
table.AppendChild(props);
var tr = new TableRow();
var tc_dis = new TableCell();
var tc_zet = new TableCell();
var tc_scor = new TableCell();
tc_dis.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { "Наименование дисциплин" },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tc_zet.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { "Количество з.е." },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tc_scor.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { "Итоговая оценка" },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tr.AppendChild(tc_dis);
tr.AppendChild(tc_zet);
tr.AppendChild(tc_scor);
table.AppendChild(tr);
foreach (var studScore in info.StudentScores)
{
tr = new TableRow();
tc_dis = new TableCell();
tc_zet = new TableCell();
tc_scor = new TableCell();
tc_dis.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { studScore.DisciplineName },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tc_zet.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { Convert.ToString(studScore.Zet) + " з.е." },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tc_scor.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { studScore.Scores },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tr.AppendChild(tc_dis);
tr.AppendChild(tc_zet);
tr.AppendChild(tc_scor);
table.AppendChild(tr);
}
var tr2 = new TableRow();
var tc_sr = new TableCell();
var tc_ss = new TableCell();
tc_sr.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { "Всего" },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tc_ss.Append(CreateParagraph(new WordParagraph
{
Texts = new List<string> { Convert.ToString(info.StudentScores[info.StudentScores.Count - 1].Average) + " з.е." },
TextProperties = new WordParagraphProperties
{
Bold = false,
Size = "24",
JustificationValues = JustificationValues.Both
}
}));
tr2.AppendChild(tc_sr);
tr2.AppendChild(tc_ss);
table.AppendChild(tr2);
docBody.AppendChild(table);
}
docBody.AppendChild(CreateSectionProperties());
wordDocument.MainDocumentPart.Document.Save();
}
}
private static SectionProperties CreateSectionProperties()
{
SectionProperties properties = new SectionProperties();
PageSize pageSize = new PageSize { Orient = PageOrientationValues.Portrait };
properties.AppendChild(pageSize);
return properties;
}
private static Paragraph CreateParagraph(WordParagraph paragraph)
{
if (paragraph != null)
{
Paragraph docParagraph = new Paragraph();
docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties));
foreach (var run in paragraph.Texts)
{
Run docRun = new Run();
RunProperties properties = new RunProperties();
properties.AppendChild(new FontSize { Val = paragraph.TextProperties.Size });
if (!run.StartsWith(" - ") && paragraph.TextProperties.Bold)
{
properties.AppendChild(new Bold());
}
docRun.AppendChild(properties);
docRun.AppendChild(new Text { Text = run, Space = SpaceProcessingModeValues.Preserve });
docParagraph.AppendChild(docRun);
}
return docParagraph;
}
return null;
}
private static ParagraphProperties CreateParagraphProperties(WordParagraphProperties paragraphProperties)
{
if (paragraphProperties != null)
{
ParagraphProperties properties = new ParagraphProperties();
properties.AppendChild(new Justification() { Val = paragraphProperties.JustificationValues });
properties.AppendChild(new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Auto });
properties.AppendChild(new Indentation());
ParagraphMarkRunProperties paragraphMarkRunProperties = new ParagraphMarkRunProperties();
if (!string.IsNullOrEmpty(paragraphProperties.Size))
{
paragraphMarkRunProperties.AppendChild(new FontSize { Val = paragraphProperties.Size });
}
if (paragraphProperties.Bold)
{
paragraphMarkRunProperties.AppendChild(new Bold());
}
properties.AppendChild(paragraphMarkRunProperties);
return properties;
}
return null;
}
}
}