Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StamAcasa.Api.Services.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StamAcasa.Api.Controllers {
[Route("api/[controller]")]
[ApiController]
public class ExcelController : ControllerBase {
IAnswersExcelExporter AnswersExcelExporter { get; }
class Form {
public int? formId { get; set; }
public long? timestamp { get; set; }
public int? age { get; set; }
public bool? isSomeTestBoolean { get; set; }
public DateTime? someDate { get; set; }
public string name { get; set; }
public List<Answer> answers { get; set; } = new List<Answer>();
}
class Answer {
public string id { get; set; }
public string questionText { get; set; }
public string answer { get; set; }
}
public ExcelController(
IAnswersExcelExporter answersExcelExporter
) {
AnswersExcelExporter = answersExcelExporter;
}
[HttpGet]
public IActionResult Get() {
var forms = new List<Form>() {
new Form() {
formId = 1,
timestamp = 20200327193822,
age = 37,
isSomeTestBoolean = true,
someDate = DateTime.Now,
name = "Ted",
answers = {
new Answer() {
id = "101",
questionText = "Când ați intrat in contact cu alte persoane ultima data?",
answer = "Ieri"
},
new Answer() {
id = "102",
questionText = "Ce simptome aveți?",
answer = "Tuse seacă, febră"
},
new Answer() {
id = "103",
questionText = "Intrebarea numarul 3?",
answer = "Da"
}
}
},
new Form() {
formId = 2,
timestamp = 20200328243524,
age = 22,
answers = {
new Answer() {
id = "104",
questionText = "Când ați intrat in contact cu alte persoane ultima data?",
answer = "Astăzi"
}
}
}
};
var jObjectsSerialized = JsonConvert.SerializeObject(forms);
var jArray = JArray.Parse(jObjectsSerialized);
var fileBinary = AnswersExcelExporter.AnswersToExcel(jArray, AnswersExcelTemplateTypes.MultipleRowsPerForm);
return File(fileBinary, "application/vnd.ms-excel", "Answers.xlsx");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment