diff --git a/Controllers/RapportiniController.cs b/Controllers/RapportiniController.cs index bdbabe1..35d0ce4 100644 --- a/Controllers/RapportiniController.cs +++ b/Controllers/RapportiniController.cs @@ -2,6 +2,7 @@ using DocumentFormat.OpenXml.Spreadsheet; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; +using System.Diagnostics; using VirtualTask.Models; using X.PagedList; @@ -85,7 +86,7 @@ namespace VirtualTask.Controllers } } - public IActionResult ExportDataToExcel(DateTime DataIni, DateTime DataFin) + public IActionResult ExportDataToExcel(DateTime dataIni, DateTime dataFin) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); @@ -110,23 +111,24 @@ namespace VirtualTask.Controllers if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; + modelList = JsonConvert.DeserializeObject>(data); + if (!dataFin.ToString().Substring(0, 10).Equals("01/01/0001")) + { + modelList = modelList.Where(x => x.data_rapportino.GetValueOrDefault().Date >= dataIni.Date && x.data_rapportino.GetValueOrDefault().Date <= dataFin.Date).ToList(); + } + else + { + modelList = modelList.Where(x => x.data_rapportino.GetValueOrDefault().Date >= dataIni.Date).ToList(); + } + var workbook = new XLWorkbook(); workbook.AddWorksheet("sheetName"); var ws = workbook.Worksheet("sheetName"); int col = 1; - - var anno = DateTime.Now.Year.ToString(); - var mese = DateTime.Now.Month.ToString(); - var giorno = DateTime.Now.Day.ToString(); - var ore = DateTime.Now.Hour.ToString(); - var minuti = DateTime.Now.Minute.ToString(); - var secondi = DateTime.Now.Second.ToString(); - - var dataCreazioneFile = anno + mese + giorno + ore + minuti + secondi; - + //Scrivo intestazioni colonne foreach (var item in modelList[0].GetType().GetProperties()) { @@ -153,12 +155,36 @@ namespace VirtualTask.Controllers row ++; } - //creo file Excel - workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\rapportini_{dataCreazioneFile}.xlsx"); - } + //Salvo il file Excel - return View(); + var anno = DateTime.Now.Year.ToString(); + var mese = DateTime.Now.Month.ToString(); + var giorno = DateTime.Now.Day.ToString(); + var ore = DateTime.Now.Hour.ToString(); + var minuti = DateTime.Now.Minute.ToString(); + var secondi = DateTime.Now.Second.ToString(); + + var dateFile = anno + mese + giorno + ore + minuti + secondi; + + workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\rapportini_{dateFile}.xlsx"); + + return RedirectToAction("Index"); + + } + else + { + errMes = response.Content.ReadAsStringAsync().Result; + helper.SetStringValue("errMsg", errMes); + return RedirectToAction("Error"); + } } + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + SessionHelper helper = new SessionHelper(this); + string e = helper.GetStringValue("errMsg"); + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e }); + } } } diff --git a/Views/Rapportini/Index.cshtml b/Views/Rapportini/Index.cshtml index 6fcfb70..2a7096c 100644 --- a/Views/Rapportini/Index.cshtml +++ b/Views/Rapportini/Index.cshtml @@ -20,17 +20,17 @@
@Html.TextBox("SearchString", null, new { placeholder = "Cerca rapportino", @class = "agy-form-field require" })
 
-
-
-
@Html.TextBox("DataIni", null, new { type="date", @class = "agy-form-field require" })
-
@Html.TextBox("DataFin", null, new { type="date", @class = "agy-form-field require" })
- -
- @Html.ActionLink("Crea Excel", "ExportDataToExcel", "Rapportini", new { DataIni = "DataIni", DataFin = "DataFin" }, null) -
+ } +
+ @using (Html.BeginForm("ExportDataToExcel", "Rapportini")) + { +
@Html.TextBox("dataIni", null, new { type = "date", @class = "agy-form-field require" })
+
@Html.TextBox("dataFin", null, new { type = "date", @class = "agy-form-field require" })
- @*
@Html.ActionLink("Crea Excel", "ExportDataToExcel", "Rapportini", new { type = "submit" })
*@ +
+ +
}