The database has a table of test results in the Russian language and математике (SubjectCode=2) :
In the second table are just information about schools:
dbo.School
Such results are 40.000-70.000. It is necessary to get the output for each test person here such a report in the pdf-файле :
My decision:
- Created an Excel template;
- I take data from the database and put it into this xlsx-template;
- I save this template to pdf-file;
- And so for each student.
LearnerReport.cs
namespace so16092016.Models { public class LearnerReport { public string SNS { get; set; } //Surname Name SecondName public string SchoolName { get; set; } public string ClassName { get; set; } public int TestResult5 { get; set; } } } Program.cs
using Excel = Microsoft.Office.Interop.Excel; namespace so16092016 { class Program { static void Main(string[] args) { resultsEntities context = new resultsEntities(); ResultsRepository resultsRepository = new ResultsRepository(context); var ma_results = resultsRepository.GetTList().Where(x => x.SubjectCode == 2); //получить результаты по математике Excel.Application app = new Excel.Application(); app.DisplayAlerts = false; Excel.Workbook book_template = app.Workbooks.Open(@"шаблон_отчета.xlsx"); Excel._Worksheet sheet_template = book_template.Sheets["отчет"]; foreach(var ob in ma_results) { //1. Создаем объкт LearnerReport из БД LearnerReport report = new LearnerReport { SNS = $"{ob.surname} {ob.name} {ob.SecondName}", SchoolName = ob.SchoolName, ClassName = ob.ClassName, TestResult5 = ob.TestResult5 }; //2. Экспорт объкта LearnerReport в шаблон xlsx sheet_template.Range["C4"].Value2 = report.SNS; sheet_template.Range["C5"].Value2 = report.SchoolName; sheet_template.Range["C6"].Value2 = report.ClassName; sheet_template.Range["C9"].Value2 = report.TestResult5; //3. Сохраняем полученный файл в .pdf на рабочем столе string file_name = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\{report.SNS}.pdf"; sheet_template.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, file_name); } book_template.Close(0); book_template = null; app.Quit(); app = null; } } } Necessary: The application works and gives the desired result. But you probably see that it is far from the PLO ( SOLID ) and as a result it is very difficult to “finish” it and scale it. Help to design this mechanism for generating such reports correctly:
- Should the model export logic in .xlsx be at the model itself or is it necessary to create a separate class manager for this?
- What should be the model?
- How to create a model report based on database objects?
- which generative pattern is more appropriate here?



