ancora update gest dataser report

This commit is contained in:
Samuele Locatelli
2023-05-25 20:00:09 +02:00
parent e27aff7ad2
commit ab3acadafe
7 changed files with 133 additions and 35 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>WebDoorCreator - Egalware</i>
<h4>Version: 0.9.2305.2517</h4>
<h4>Version: 0.9.2305.2519</h4>
<br /> Release note:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
0.9.2305.2517
0.9.2305.2519
+2 -2
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>0.9.2305.2517</version>
<url>http://nexus.steamware.net/repository/SWS/WDC/stable/WDC.API.zip</url>
<version>0.9.2305.2519</version>
<url>http://nexus.steamware.net/repository/SWS/WDC/stable/WDC.UI.zip</url>
<changelog>http://nexus.steamware.net/repository/SWS/WDC/stable/ChangeLog.html</changelog>
<mandatory>false</mandatory>
</item>
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Reporting.NETCore;
using NLog;
using System.Data;
using WebDoorCreator.Core.ReportViewer;
using WebDoorCreator.Data.Services;
namespace WebDoorCreator.API.Controllers
@@ -30,25 +33,32 @@ namespace WebDoorCreator.API.Controllers
return "OK";
}
[HttpGet("GetReport")]
public async Task<IActionResult> GetReport(int OrderId, string Format)
/// <summary>
/// Restituisce report dato ordine e formato
/// </summary>
/// <param name="OrderId">ID univoco ordine</param>
/// <param name="Format">Formato: PDF/HTML/DOCX/XLSX</param>
/// <returns></returns>
[HttpGet("GetOrderReport")]
public async Task<IActionResult> GetOrderReport(int OrderId, string Format)
{
await Task.Delay(1);
string FileName = $"DCA-Order-{OrderId:00000000}";
string Extension = "";
string MimeType = "";
switch (Format.ToUpper())
{
case "HTML5":
case "HTML":
Extension = "html";
MimeType = "text/html";
break;
case "WORDOPENXML":
case "DOCX":
Extension = "docx";
MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case "EXCELOPENXML":
case "XLSX":
Extension = "xlsx";
MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
@@ -60,7 +70,7 @@ namespace WebDoorCreator.API.Controllers
break;
}
// restituisco oggetto
return PrepareReport(Format, Extension, MimeType);
return OrderReportGet(FileName, Format, Extension, MimeType);
}
#endregion Public Methods
@@ -75,26 +85,66 @@ namespace WebDoorCreator.API.Controllers
#region Private Properties
private decimal GizmoPrice { get; set; }
private QueueDataService QDataServ { get; set; } = null!;
private decimal WidgetPrice { get; set; }
#endregion Private Properties
#region Private Methods
private IActionResult PrepareReport(string renderFormat, string extension, string mimeType)
/// <summary>
/// Restituisce filestream del report richiesto
/// </summary>
/// <param name="FileName">Nome file desiderato x download</param>
/// <param name="renderFormat"></param>
/// <param name="extension"></param>
/// <param name="mimeType"></param>
/// <returns></returns>
private IActionResult OrderReportGet(string FileName, string renderFormat, string extension, string mimeType)
{
using var report = new LocalReport();
using (var report = new LocalReport())
{
// setup parametri
List<ReportParameter> RepParams = new List<ReportParameter>();
RepParams.Add(new ReportParameter("Title", "Invoice 4/2020"));
// setup DsReport
Dictionary<string, DataTable> DsDict = new Dictionary<string, DataTable>();
Random rnd = new Random();
decimal WidgetPrice = 204.99m;
decimal GizmoPrice = 2.41m;
ReportItem[] items = new[] {
new ReportItem { Description = "Widget 6000", Price = WidgetPrice, Qty = rnd.Next(1,5) },
new ReportItem { Description = "Gizmo MAX", Price = GizmoPrice, Qty = rnd.Next(10,50) },
new ReportItem { Description = "Pippo", Price = 1.2m, Qty = rnd.Next(3,9) },
new ReportItem { Description = "Paperino", Price = 1.74m, Qty = rnd.Next(5,15) }
};
DataTable tabDati = new DataTable("Items");
WidgetPrice = 104.99m;
GizmoPrice = 1.41m;
//tabDati = items.ToDataTable();
Core.ReportViewer.Report.Load(report, WidgetPrice, GizmoPrice);
var pdf = report.Render(renderFormat);
return File(pdf, mimeType, "report." + extension);
tabDati.Columns.Add("Description", typeof(string));
tabDati.Columns.Add("Price", typeof(decimal));
tabDati.Columns.Add("Qty", typeof(int));
tabDati.Columns.Add("Total", typeof(decimal));
DataRow newRow = tabDati.NewRow();
foreach (var item in items)
{
newRow = tabDati.NewRow();
newRow["Description"] = item.Description;
newRow["Price"] = item.Price;
newRow["Qty"] = item.Qty;
newRow["Total"] = item.Price*item.Qty;
// Add the row to the rows collection.
tabDati.Rows.Add(newRow);
}
// aggiungo dizionario
DsDict.Add("Items", tabDati);
// carico dati nel report...
Core.ReportViewer.Report.Load(report, "Report.rdlc", RepParams, DsDict);
var rawData = report.Render(renderFormat);
return File(rawData, mimeType, $"{FileName}.{extension}");
}
}
#endregion Private Methods
+27 -12
View File
@@ -2,6 +2,7 @@
using Microsoft.ReportingServices.ReportProcessing.ReportObjectModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
@@ -9,27 +10,41 @@ namespace WebDoorCreator.Core.ReportViewer
{
public class Report
{
public static void Load(LocalReport report, decimal widgetPrice, decimal gizmoPrice)
/// <summary>
/// Effettua setup del Report corrente
/// </summary>
/// <param name="ReportObj">Oggetto LocalReport da impiegare</param>
/// <param name="FileNameRdlc">Nome del Report (file rdlc) da usare</param>
/// <param name="RepParams">Parametri opzionali report</param>
/// <param name="ReportDatasets">Dizionario dei dataset del report (DsName + DataTable relativa)</param>
public static void Load(LocalReport ReportObj, string FileNameRdlc, List<ReportParameter> RepParams, Dictionary<string, DataTable> ReportDatasets)
{
var items = new[] { new ReportItem { Description = "Widget 6000", Price = widgetPrice, Qty = 1 }, new ReportItem { Description = "Gizmo MAX", Price = gizmoPrice, Qty = 25 } };
var parameters = new[] { new ReportParameter("Title", "Invoice 4/2020") };
// setup preliminari
ReportObj.EnableExternalImages = true;
// calcolo path complessivo
string reportPath = Path.Combine(AppContext.BaseDirectory, "Reports", FileNameRdlc);
/*------------------------------------------------------------------------------------------
* NB: SE volessi includere i report come EMBEDDED potrei usare una versione alternativa,
* ad esempio (x report inclusi in area WebDoorCreator.Core\ReportViewer\Report.rdlc
* NB: SE volessi includere i ReportObj come EMBEDDED potrei usare una versione alternativa,
* ad esempio (x ReportObj inclusi in area WebDoorCreator.Core\ReportViewer\Report.rdlc
*
* using var rs = Assembly.GetExecutingAssembly().GetManifestResourceStream("WebDoorCreator.Core.ReportViewer.Report.rdlc");
* report.LoadReportDefinition(rs);
* ReportObj.LoadReportDefinition(rs);
*
* oppure con filestream
* using (FileStream fs = File.OpenRead(reportPath))
* {
* ReportObj.LoadReportDefinition(fs);
* ....
* }
*
* ------------------------------------------------------------------------------------------*/
string reportPath = Path.Combine(AppContext.BaseDirectory, "Reports", "Report.rdlc");
using (FileStream fs = File.OpenRead(reportPath))
ReportObj.ReportPath = reportPath;
foreach (var item in ReportDatasets)
{
report.LoadReportDefinition(fs);
report.DataSources.Add(new ReportDataSource("Items", items));
report.SetParameters(parameters);
ReportObj.DataSources.Add(new ReportDataSource(item.Key, item.Value));
}
ReportObj.SetParameters(RepParams);
}
}
}
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebDoorCreator.Core.ReportViewer
{
public static class ReportExtUtils
{
public static DataTable ToDataTable<T>(this T[] genList)
{
if (genList == null || genList.Length == 0) return null;
DataTable table = new DataTable();
var firstRec = genList[0];
table.Columns.AddRange(firstRec.GetType().GetFields().Select(field => new DataColumn(field.Name, field.FieldType)).ToArray());
int fieldCount = firstRec.GetType().GetFields().Count();
genList.All(singleRow =>
{
table.Rows.Add(Enumerable.Range(0, fieldCount).Select(index => singleRow.GetType().GetFields()[index].GetValue(singleRow)).ToArray());
return true;
});
return table;
}
}
}
+5 -2
View File
@@ -3,11 +3,15 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Version>0.9.2305.2517</Version>
<Version>0.9.2305.2519</Version>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-WebDoorCreator.UI-dfe95fed-1398-4144-bd43-8b3a765d6608</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Controllers\ReportController.cs" />
</ItemGroup>
<ItemGroup>
<Content Remove="compilerconfig.json" />
</ItemGroup>
@@ -50,7 +54,6 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NLog" Version="5.1.2" />
<PackageReference Include="ReportViewerCore.NETCore" Version="15.1.17" />
<PackageReference Include="StackExchange.Redis" Version="2.6.96" />
<PackageReference Include="YamlDotNet" Version="13.1.0" />
</ItemGroup>