Michele: estrazione excel

This commit is contained in:
michele 2023-12-15 17:57:29 +01:00
parent 772888c884
commit aacb8e0437
10 changed files with 372 additions and 4 deletions

View File

@ -0,0 +1,164 @@
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class RapportiniController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string tenant = string.Empty;
string errMes = string.Empty;
string admin = string.Empty;
HttpClient client;
private readonly IConfiguration _configuration;
public RapportiniController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index(string searchString, int? page = 1)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "rapportiniList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Rapportini> modelList = new List<Rapportini>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
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();
ViewData["CurrentFilter"] = searchString;
}
else
ViewData["CurrentFilter"] = null;
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortLinks = modelList
.OrderByDescending(s => s.seriale_rapportino)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
public IActionResult ExportDataToExcel(DateTime DataIni, DateTime DataFin)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "rapportiniList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Rapportini> modelList = new List<Rapportini>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapportini>>(data);
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())
{
ws.Cell(1, col).Style = ws.Cell(1, col).Style.Font.SetBold();
ws.Cell(1, col).Value = item.Name.ToString().Replace("_"," ").ToUpper();
col++;
}
var row = 0;
foreach (var item in modelList)
{
var colonna = 1;
//righe per ogni elemento nel modelList
foreach (var prop in item.GetType().GetProperties())
{
ws.Cell(row + 2, colonna).Value = prop.GetValue(item).ToString();
colonna ++;
}
row ++;
}
//creo file Excel
workbook.SaveAs($"C:\\Users\\utente\\Desktop\\ExcelVT\\rapportini_{dataCreazioneFile}.xlsx");
}
return View();
}
}
}

View File

@ -17,7 +17,7 @@ namespace VirtualTask.Models
[Display(Name = "Descrizione"), Required(ErrorMessage = "Inserire descrizione")]
public string? ccdescr { get; set; }
[Display(Name = "Desc. Supplementare"), Required(ErrorMessage = "Inserire Desc. Supplementare")]
[Display(Name = "Testo del rapportino App (desc. sup.)"), Required(ErrorMessage = "Inserire Desc. Supplementare")]
public string? ccdessup { get; set; }

36
Models/Rapportini.cs Normal file
View File

@ -0,0 +1,36 @@
namespace VirtualTask.Models
{
public class Rapportini
{
/// <summary>
/// Seriale
/// </summary>
public string? seriale_rapportino { get; set; }
/// <summary>
/// Azienda
/// </summary>
public string? azienda_impianto { get; set; }
/// <summary>
/// Codice Impianto
/// </summary>
public string? codice_impianto { get; set; }
/// <summary>
/// Seriale chiamata
/// </summary>
public string? seriale_chiamata { get; set; }
/// <summary>
/// Seriale chiamata
/// </summary>
public string? seriale_commessa { get; set; }
/// <summary>
/// Data rapportino
/// </summary>
public DateTime? data_rapportino { get; set; }
}
}

View File

@ -27,7 +27,7 @@
<div class="col-lg-6 col-md-6 col-sm-12 col-12">&nbsp;</div>
<div class="form-group">
<h5><label asp-for="ccdessup" class="agy-client-quote"></label></h5>
<input asp-for="ccdessup" class="agy-form-field require" placeholder="Descrizione Supplementare" />
<input asp-for="ccdessup" class="agy-form-field require" placeholder="Testo del rapportino (desc. sup.)" />
<span asp-validation-for="ccdessup" class="text-danger"></span>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-12">&nbsp;</div>

View File

@ -35,7 +35,7 @@
<div class="form-group">
<h5><label asp-for="ccdessup" class="agy-client-quote"></label></h5>
@* <input asp-for="ccdessup" class="form-control" /> *@
<input asp-for="ccdessup" class="agy-form-field require" class="form-control" placeholder="Descr. Supplementare" />
<input asp-for="ccdessup" class="agy-form-field require" class="form-control" placeholder="Testo del rapportino" />
<span asp-validation-for="ccdessup" class="text-danger"></span>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-12">&nbsp;</div>

View File

@ -31,7 +31,7 @@
<th>Azienda</th>
<th>Codice</th>
<th>Descrizione</th>
<th>Desc. Sup.</th>
<th>Testo del rapportino App (desc. sup.)</th>
<th></th>
</tr>
</thead>

View File

@ -0,0 +1,165 @@
@model IPagedList<VirtualTask.Models.Rapportini>
@using X.PagedList;
@using X.PagedList.Mvc.Core;
@using X.PagedList.Web.Common;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewData["Title"] = "Rapportini";
Layout = "~/Views/Shared/_LayoutAreaRiservata.cshtml";
}
<div class="agy-project-wrapper agy-project-page-wrapper">
<div class="container">
<div class="row">
@using (Html.BeginForm())
{
<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:2%;">&nbsp;</div>
<div style="float:left;width:57%;"><input type="submit" value="Cerca" class="agy-btn submitForm" /></div>
<hr/>
<hr/>
<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;">
@Html.ActionLink("Crea Excel", "ExportDataToExcel", "Rapportini", new { DataIni = "DataIni", DataFin = "DataFin" }, null)
</div>
</div>
@* <div style="float:left;width:40%;padding:0 20px;">@Html.ActionLink("Crea Excel", "ExportDataToExcel", "Rapportini", new { type = "submit" })</div> *@
}
<table class="table">
<thead>
<tr>
<th>Seriale Rapportino</th>
<th>Azienda impianto</th>
<th>Codice impianto</th>
<th>Seriale chiamate</th>
<th>Seriale commessa</th>
<th>Data rapportino</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>
<a href="@Url.Action("Edit", "Anag", new { id=item.seriale_rapportino })" title="Modifica" class="links">
<img alt="Modifica" src="@Url.Content("~/assets/images/icons8-modificare-64.png")" style="width:30px;height:30px;">
</a>
|
<a href="@Url.Action("Details", "Anag", new { id=item.seriale_rapportino })" title="Dettaglio" class="links">
<img alt="Dettaglio" src="@Url.Content("~/assets/images/icons8-visualizza-file-64.png")" style="width:30px;height:30px;">
</a>
|
<a href="@Url.Action("Delete", "Anag", new { id=item.seriale_rapportino })" title="Elimina" class="links">
<img alt="Elimina" src="@Url.Content("~/assets/images/icons8-elimina-50.png")" style="width:30px;height:30px;">
</a>
</td> *@
</tr>
}
</tbody>
</table>
<br />
<nav>
@Html.PagedListPager(Model, page => Url.Action("index", new { page = page, searchString = @ViewData["CurrentFilter"] }), new PagedListRenderOptions()
{
ActiveLiElementClass = "active",
PageClasses = new[] { "page-link" },
LiElementClasses = new[] { "page-item" },
UlElementClasses = new[] { "pagination", "justify-content-center", "mt-3" },
LinkToNextPageFormat = "Successiva",
LinkToPreviousPageFormat = "Precedente",
MaximumPageNumbersToDisplay = 5,
DisplayLinkToPreviousPage = PagedListDisplayMode.Always,
DisplayLinkToNextPage = PagedListDisplayMode.Always
})
</nav>
</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>
*@

View File

@ -160,6 +160,7 @@ Purchase:
<li><a asp-area="" asp-controller="AziendaRif" asp-action="Index">Dati Azienda</a></li>
<li><a asp-area="" asp-controller="DatiAzienda" asp-action="Index">Dati Azienda2</a></li>
<li><a asp-area="" asp-controller="CommesseVT" asp-action="Index">Commesse</a></li>
<li><a asp-area="" asp-controller="Rapportini" asp-action="Index">Elenco rapportini</a></li>
<li><a asp-area="" asp-controller="Login" asp-action="Logout">Logout</a></li>
@{

View File

@ -7,6 +7,8 @@
<PublishSingleFile>false</PublishSingleFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.102.1" />
<PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="X.PagedList.Mvc.Core" Version="8.4.7" />

BIN
yourExcel.xlsx Normal file

Binary file not shown.