VirtualTask/Controllers/RapportiniController.cs
2023-12-15 17:57:29 +01:00

165 lines
5.5 KiB
C#

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();
}
}
}