add not tested table creation
This commit is contained in:
parent
1baf69234a
commit
bf02438b2c
3 changed files with 71 additions and 0 deletions
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>
|
||||
48
SurveyLib.Tools/Tools/TableExporter.cs
Normal file
48
SurveyLib.Tools/Tools/TableExporter.cs
Normal file
|
|
@ -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<byte[]> 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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue