191 lines
6.6 KiB
C#
191 lines
6.6 KiB
C#
using ClosedXML.Excel;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using System.Diagnostics;
|
|
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);
|
|
|
|
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;
|
|
|
|
//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 ++;
|
|
}
|
|
|
|
//Salvo il file Excel
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|