Michele: Filtro impianto per estrazioni excel

This commit is contained in:
michele 2023-12-28 16:17:25 +01:00
parent 17db3fbd41
commit 017ccb4efb
3 changed files with 102 additions and 84 deletions

View File

@ -1,6 +1,8 @@
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
//using AspNetCore;
using ClosedXML.Excel;
//using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using System.Diagnostics;
using VirtualTask.Models;
@ -29,8 +31,9 @@ namespace VirtualTask.Controllers
apiUrl = key;
}
#region INDEX
public IActionResult Index(string searchString, int? page = 1)
public IActionResult Index(string Impianto, int? page = 1)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
@ -56,11 +59,15 @@ namespace VirtualTask.Controllers
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapportini>>(data);
if (!string.IsNullOrEmpty(searchString))
{
modelList = modelList.Where(s => s.seriale_rapportino.ToUpper().Contains(searchString.ToUpper())).ToList();
ViewBag.Rapportini = getImpianti();
ViewData["CurrentFilter"] = searchString;
if (!string.IsNullOrEmpty(Impianto))
{
modelList = modelList.Where(s => s.codice_impianto.Contains(Impianto)).ToList();
ViewData["CurrentFilter"] = Impianto;
ViewBag.Impianto = Impianto;
}
else
ViewData["CurrentFilter"] = null;
@ -86,7 +93,12 @@ namespace VirtualTask.Controllers
}
}
public IActionResult ExportDataToExcel(DateTime dataIni, DateTime dataFin)
#endregion INDEX
#region metodi interni
[HttpPost]
public IActionResult ExportDataToExcel(DateTime dataIni, DateTime dataFin, string Impianto)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
@ -106,6 +118,8 @@ namespace VirtualTask.Controllers
List<Rapportini> modelList = new List<Rapportini>();
//modelList = lista;
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
@ -114,6 +128,11 @@ namespace VirtualTask.Controllers
modelList = JsonConvert.DeserializeObject<List<Rapportini>>(data);
//ViewData["CurrentFilter"] = Impianto;
modelList = modelList.Where(x => x.codice_impianto.Equals(Impianto)).ToList();
//ViewBag.Impianto = Impianto;
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();
@ -128,12 +147,12 @@ namespace VirtualTask.Controllers
var ws = workbook.Worksheet("sheetName");
int col = 1;
//Scrivo intestazioni colonne
foreach (var item in modelList[0].GetType().GetProperties())
{
ws.Cell(1, col).Style = ws.Cell(1, col).Style.Font.SetBold();
ws.Cell(1, col).Value = item.Name.ToString().Replace("_"," ").ToUpper();
ws.Cell(1, col).Value = item.Name.ToString().Replace("_", " ").ToUpper();
col++;
}
@ -149,14 +168,13 @@ namespace VirtualTask.Controllers
{
ws.Cell(row + 2, colonna).Value = prop.GetValue(item).ToString();
colonna ++;
colonna++;
}
row ++;
row++;
}
//Salvo il file Excel
var anno = DateTime.Now.Year.ToString();
var mese = DateTime.Now.Month.ToString();
var giorno = DateTime.Now.Day.ToString();
@ -166,10 +184,9 @@ namespace VirtualTask.Controllers
var dateFile = anno + mese + giorno + ore + minuti + secondi;
workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\rapportini_{dateFile}.xlsx");
workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\buoni_{dateFile}.xlsx");
return RedirectToAction("Index");
}
else
{
@ -179,6 +196,46 @@ namespace VirtualTask.Controllers
}
}
private List<SelectListItem> getImpianti()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
//urlBase = apiUrl + "impiantiListMngr";
urlBase = apiUrl + "rapportiniList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List</*Impianto*/Rapportini> modelList = new List</*Impianto*/Rapportini>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List</*Impianto*/Rapportini>>(data);
//per gestire primo elemento tendina (deve essere vuoto)
SelectListItem listItemFirt = new SelectListItem();
listItemFirt.Value = string.Empty;
listItemFirt.Text = " - Impianto";
selectItems.Add(listItemFirt);
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
//string s = role.imcodimp + " - " + role.imdescri;
//listItem.Value = role.imcodimp;
string s = role.codice_impianto;
listItem.Value = role.codice_impianto;
listItem.Text = s;
selectItems.Add(listItem);
}
}
return selectItems;
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
@ -186,5 +243,8 @@ namespace VirtualTask.Controllers
string e = helper.GetStringValue("errMsg");
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e });
}
#endregion metodi interni
}
}

View File

@ -1,4 +1,6 @@
namespace VirtualTask.Models
using System.ComponentModel.DataAnnotations;
namespace VirtualTask.Models
{
public class Rapportini
{
@ -15,6 +17,7 @@
/// <summary>
/// Codice Impianto
/// </summary>
[Display(Name = "Impianto")]
public string? codice_impianto { get; set; }

View File

@ -6,7 +6,7 @@
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewData["Title"] = "Rapportini";
ViewData["Title"] = "Esporta buoni su Excel";
Layout = "~/Views/Shared/_LayoutAreaRiservata.cshtml";
}
@ -14,24 +14,39 @@
<div class="agy-project-wrapper agy-project-page-wrapper">
<div class="container">
<div class="row">
@using (Html.BeginForm())
@using (Html.BeginForm("Index", "Rapportini"))
{
<div>
<div style="float:left;width:40%;padding:0 20px;">@Html.TextBox("SearchString", null, new { placeholder = "Cerca rapportino", @class = "agy-form-field require" })</div>
<div style="float:left;width:40%;padding:0 20px;">
<th><b>Impianto:</b></th>
@Html.DropDownList("Impianto", ViewBag.Rapportini, null, new { @class = "agy-form-field require" })
<div style="float:left;width:57%;"><input type="submit" value="Cerca" class="agy-btn submitForm" /></div>
</div>
<div style="float:left;width:2%;">&nbsp;</div>
<div style="float:left;width:2%;">&nbsp;</div>
<div style="float:left;width:2%;">&nbsp;</div>
<div style="float:left;width:57%;"><input type="submit" value="Cerca" class="agy-btn submitForm" /></div>
</div>
}
<br>
<br>
<br>
@using (Html.BeginForm("ExportDataToExcel", "Rapportini"))
{
<div style="float:left;width:40%;padding:0 20px;">@Html.TextBox("dataIni", null, new { type = "date", @class = "agy-form-field require" })</div>
<div style="float:left;width:40%;padding:0 20px;">@Html.TextBox("dataFin", null, new { type = "date", @class = "agy-form-field require" })</div>
<div style="float:left;width:40%;padding:0 20px;">
<input type="submit" name="estrai" class="agy-btn submitForm" />
<th><b>Data da:</b></th>
@Html.TextBox("dataIni", null, new { type = "date", @class = "agy-form-field require" })
</div>
<div style="float:left;width:40%;padding:0 20px;">
<th><b>Data a:</b></th>
@Html.TextBox("dataFin", null, new { type = "date", @class = "agy-form-field require" })
</div>
@Html.Hidden("Impianto", ViewBag.Rapportini, null)
<div style="float:left;width:40%;padding:0 20px;">
<input type="submit" value="Esporta excel" name="Esporta excel" class="agy-btn submitForm" />
</div>
}
<table class="table">
@ -103,63 +118,3 @@
</div>
</div>
</div>
@* <p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.seriale_rapportino)
</th>
<th>
@Html.DisplayNameFor(model => model.azienda_impianto)
</th>
<th>
@Html.DisplayNameFor(model => model.codice_impianto)
</th>
<th>
@Html.DisplayNameFor(model => model.seriale_chiamata)
</th>
<th>
@Html.DisplayNameFor(model => model.seriale_commessa)
</th>
<th>
@Html.DisplayNameFor(model => model.data_rapportino)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.seriale_rapportino)
</td>
<td>
@Html.DisplayFor(modelItem => item.azienda_impianto)
</td>
<td>
@Html.DisplayFor(modelItem => item.codice_impianto)
</td>
<td>
@Html.DisplayFor(modelItem => item.seriale_chiamata)
</td>
<td>
@Html.DisplayFor(modelItem => item.seriale_commessa)
</td>
<td>
@Html.DisplayFor(modelItem => item.data_rapportino)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
*@