Merge branch 'unstable' into 'main'
Add excel export functionality See merge request internship-2025/survey-webapp/surveylib!22
This commit is contained in:
commit
aae9d32397
4 changed files with 102 additions and 0 deletions
6
SurveyLib.Core/Tools/IDataExporter.cs
Normal file
6
SurveyLib.Core/Tools/IDataExporter.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
namespace SurveyLib.Core.Tools;
|
||||
|
||||
public interface IDataExporter
|
||||
{
|
||||
Task<byte[]> ExportDataBySurveyIdAsync(int surveyId);
|
||||
}
|
||||
17
SurveyLib.Tools/SurveyLib.Tools.csproj
Normal file
17
SurveyLib.Tools/SurveyLib.Tools.csproj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SurveyLib.Core\SurveyLib.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ClosedXML" Version="0.105.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
73
SurveyLib.Tools/Tools/TableExporter.cs
Normal file
73
SurveyLib.Tools/Tools/TableExporter.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
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;
|
||||
private readonly ICompletionService _completionService;
|
||||
private readonly IAnswerService _answerService;
|
||||
|
||||
public TableExporter(ISurveyService surveyService, IQuestionService questionService,
|
||||
ICompletionService completionService, IAnswerService answerService)
|
||||
{
|
||||
_surveyService = surveyService;
|
||||
_questionService = questionService;
|
||||
_completionService = completionService;
|
||||
_answerService = answerService;
|
||||
}
|
||||
|
||||
public async Task<byte[]> ExportDataBySurveyIdAsync(int surveyId)
|
||||
{
|
||||
var survey = await _surveyService.GetSurveyAsync(surveyId);
|
||||
var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId);
|
||||
var completions = await _completionService.GetCompletionsBySurveyIdAsync(surveyId);
|
||||
|
||||
using var workbook = new XLWorkbook();
|
||||
var ws = workbook.Worksheets.Add(survey.Title);
|
||||
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 = "Export issued 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++;
|
||||
}
|
||||
|
||||
var row = 5;
|
||||
foreach (var completion in completions)
|
||||
{
|
||||
ws.Cell(row, 1).Value = completion.CompletedBy?.ToString() ?? "Unknown";
|
||||
ws.Cell(row, 2).Value = completion.FinishedAt;
|
||||
column = 3;
|
||||
var answers = await _answerService.GetAnswersByCompletionIdAsync(completion.Id);
|
||||
var answersGroupedByQuestion = answers.GroupBy(a => a.QuestionId);
|
||||
foreach (var questionAnswers in answersGroupedByQuestion)
|
||||
{
|
||||
ws.Cell(row, column).Value = string.Join("; ", questionAnswers.Select(a => a.AnswerText));
|
||||
column++;
|
||||
}
|
||||
|
||||
row++;
|
||||
}
|
||||
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
workbook.SaveAs(stream);
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue