Files
Asegasa.NET/bdAsegasa/Informes/Informes.cs
2026-04-28 11:52:16 +02:00

106 lines
3.2 KiB
C#
Raw Permalink Blame History

using bdAsegasa.db;
using bdAsegasa.dbcontext;
using DevExpress.XtraReports.UI;
using Microsoft.EntityFrameworkCore;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using tsEFCore8;
using tsUtilidades;
using static Google.Protobuf.WellKnownTypes.FieldMask;
namespace bdAsegasa.Informes
{
public class Informes
{
public static void ImprimeInforme(XtraReport xr, object Datos, bool ImpresoraPredeterminada)
{
foreach (DevExpress.XtraReports.Parameters.Parameter pr in xr.Parameters)
{
pr.Visible = false;
}
xr.DataSource = Datos;
xr.CreateDocument(true);
if (ImpresoraPredeterminada)
{
xr.Print();
}
else
{
xr.Print();
}
}
public static MemoryStream GeneraXLSX(XtraReport xr, object Datos)
{
MemoryStream ms = new MemoryStream();
foreach (DevExpress.XtraReports.Parameters.Parameter pr in xr.Parameters)
{
pr.Visible = false;
}
xr.DataSource = Datos;
xr.CreateDocument(true);
xr.ExportToXlsx(ms);
return ms;
}
public static MemoryStream GeneraPDF(bdAsegasa.tscgestionasegasa bd, int idPlantilla, object Datasource)
{
try
{
if (idPlantilla == 0)
throw new Exception("Seleccione primero la plantilla a imprimir");
XtraReport xr = new XtraReport();
var plantillas = bd.plantillas.ToList();
var pl = plantillas.Where(x => x.idPlantilla == idPlantilla).First();
string s = Encoding.UTF8.GetString(pl.idFicheroNavigation.Fichero);
using (MemoryStream tempMs = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(tempMs, Encoding.UTF8, 1024, true))
{
sw.Write(s);
sw.Flush();
tempMs.Position = 0;
xr = XtraReport.FromStream(tempMs, true);
}
}
try
{
var pr = xr.Parameters.Cast<DevExpress.XtraReports.Parameters.Parameter>().FirstOrDefault(p => p.Name == "Fecha");
if (pr != null)
{
pr.Value = DateTime.Now;
pr.Visible = false;
}
}
catch
{
// Ignorar si el par<61>metro no existe
}
xr.DataSource = Datasource;
xr.CreateDocument(true);
MemoryStream ms = new MemoryStream();
xr.ExportToPdf(ms);
ms.Position = 0; // Importante para que quien reciba el stream pueda leerlo desde el inicio
return ms;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
}