added working excel export
This commit is contained in:
parent
6996ca6502
commit
5f67856119
1 changed files with 28 additions and 3 deletions
|
|
@ -8,27 +8,33 @@ 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)
|
||||
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("Sheet1");
|
||||
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 = "Created At";
|
||||
ws.Cell(3, 1).Value = "Export issued at";
|
||||
ws.Cell(3, 2).Value = DateTime.UtcNow;
|
||||
|
||||
ws.Cell(4, 1).Value = "Completed By";
|
||||
|
|
@ -41,6 +47,25 @@ public class TableExporter : IDataExporter
|
|||
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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue