From 86e3fcd8444c416691d5d812dd40ccc7fe3688fd Mon Sep 17 00:00:00 2001 From: shept Date: Mon, 9 Jun 2025 01:40:16 +0500 Subject: [PATCH] add not tested table creation --- SurveyLib.Tools/SurveyLib.Tools.csproj | 17 +++++++++ SurveyLib.Tools/Tools/TableExporter.cs | 48 ++++++++++++++++++++++++++ SurveyLib.sln | 6 ++++ 3 files changed, 71 insertions(+) create mode 100644 SurveyLib.Tools/SurveyLib.Tools.csproj create mode 100644 SurveyLib.Tools/Tools/TableExporter.cs diff --git a/SurveyLib.Tools/SurveyLib.Tools.csproj b/SurveyLib.Tools/SurveyLib.Tools.csproj new file mode 100644 index 0000000..ed940b2 --- /dev/null +++ b/SurveyLib.Tools/SurveyLib.Tools.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/SurveyLib.Tools/Tools/TableExporter.cs b/SurveyLib.Tools/Tools/TableExporter.cs new file mode 100644 index 0000000..20d8055 --- /dev/null +++ b/SurveyLib.Tools/Tools/TableExporter.cs @@ -0,0 +1,48 @@ +using ClosedXML.Excel; +using SurveyLib.Core.Services; +using SurveyLib.Core.Tools; + +namespace SurveyLib.Tools.Tools; + +public class TableExporter : IDataExporter +{ + private readonly ISurveyService _surveyService; + private readonly IQuestionService _questionService; + + public TableExporter(ISurveyService surveyService, IQuestionService questionService) + { + _surveyService = surveyService; + _questionService = questionService; + } + + public async Task ExportDataBySurveyIdAsync(int surveyId) + { + var survey = await _surveyService.GetSurveyAsync(surveyId); + var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId); + + using var workbook = new XLWorkbook(); + var ws = workbook.Worksheets.Add("Sheet1"); + ws.Cell(1, 1).Value = "Title"; + ws.Cell(1, 2).Value = survey.Title; + + ws.Cell(2, 1).Value = "Description"; + ws.Cell(2, 2).Value = survey.Description; + + ws.Cell(3, 1).Value = "Created At"; + ws.Cell(3, 2).Value = DateTime.UtcNow; + + ws.Cell(4, 1).Value = "Completed By"; + ws.Cell(4, 2).Value = "Finished At"; + + var column = 3; + foreach (var question in questions) + { + ws.Cell(4, column).Value = question.Title; + column++; + } + + using var stream = new MemoryStream(); + workbook.SaveAs(stream); + return stream.ToArray(); + } +} \ No newline at end of file diff --git a/SurveyLib.sln b/SurveyLib.sln index 46c7c02..447d250 100644 --- a/SurveyLib.sln +++ b/SurveyLib.sln @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Infrastructure.EF EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Services", "SurveyLib.Services\SurveyLib.Services.csproj", "{C57F4551-A397-45AB-8FF6-2C6400317CC6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Tools", "SurveyLib.Tools\SurveyLib.Tools.csproj", "{43E14A39-697E-4550-804F-FF937946F7E3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,9 @@ Global {C57F4551-A397-45AB-8FF6-2C6400317CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU {C57F4551-A397-45AB-8FF6-2C6400317CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU {C57F4551-A397-45AB-8FF6-2C6400317CC6}.Release|Any CPU.Build.0 = Release|Any CPU + {43E14A39-697E-4550-804F-FF937946F7E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43E14A39-697E-4550-804F-FF937946F7E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43E14A39-697E-4550-804F-FF937946F7E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43E14A39-697E-4550-804F-FF937946F7E3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal