Oracle
 SQL >> Database >  >> RDS >> Oracle

Velocizzare le esportazioni Excel lente di RDLC LocalReport con raggruppamenti dinamici

Se il tuo report ha raggruppamenti, è probabile che tu stia colpendo un problema noto: a partire da .NET 4, quando è stato rimosso il legacy CAS, i report RDLC elaborati localmente impiegano un tempo enorme per eseguire raggruppamenti dinamici o filtri dinamici. Se ne discute qui: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6d89e2ce-3528-465f-9740-7e22aa7b7aae/slow-performance-with-dynamic-grouping-and-reportviewer-in-local-mode?forum=sqlreportingservices

La soluzione migliore che ho trovato tra quelle proposte è questa.

1. Crea un nuovo progetto di libreria .NET 3.5 e aggiungi un file che svolga l'effettiva elaborazione del report.

using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

// L nuovo assembly WebReportviewer.FullTrustReportviewer
// fa solo una cosa: eseguire il report. Questo è il codice,
// che va messo in un progetto separato:
namespace WebReportviewer
{
    [Serializable]
    public class FullTrustReportviewer : MarshalByRefObject
    {
        private ReportViewer FullTrust;

        public FullTrustReportviewer()
        {
            FullTrust = new ReportViewer();
            FullTrust.ShowExportControls = false;
            FullTrust.ShowPrintButton = true;
            FullTrust.ShowZoomControl = true;
            FullTrust.SizeToReportContent = false;
            FullTrust.ShowReportBody = true;
            FullTrust.ShowDocumentMapButton = false;
            FullTrust.ShowFindControls = true;
        }

        public void Initialize(string DisplayName, string ReportPath, bool Visible,
                               ReportParameter[] reportParam, string reportRenderFormat,
                               string deviceInfo, string repMainContent,
                               List<string[]> repSubContent)
        {
            FullTrust.LocalReport.DisplayName = DisplayName;
            FullTrust.LocalReport.ReportPath = ReportPath;
            FullTrust.LocalReport.SetParameters(reportParam);
            repSubContent.ForEach(x =>
            {
                FullTrust.LocalReport.LoadSubreportDefinition(x[0], new StringReader(x[1]));
            });
            FullTrust.LocalReport.DataSources.Clear();
        }

        public byte[] Render(string reportRenderFormat, string deviceInfo)
        {
            return FullTrust.LocalReport.Render(reportRenderFormat, deviceInfo);
        }

        public void AddDataSources(string p, DataTable datatable)
        {
            FullTrust.LocalReport.DataSources.Add(new ReportDataSource(p, datatable));
        }

        public SubreportProcessingEventHandler SubreportProcessing { get; set; }

        public static void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            LocalReport lr = (LocalReport)sender;
            e.DataSources.Clear();
            ReportDataSource rds;
            if (e.ReportPath.Contains("DataTable2"))
            {
                DataTable dt = (DataTable)lr.DataSources["DataTable2"].Value;
                DataView dv = new DataView(dt);
                dv.RowFilter = string.Format("Id={0}", e.Parameters["Id"].Values[0]);
                rds = new ReportDataSource("DataTable2", dv.ToTable());
                e.DataSources.Add(rds);
            }
        }
    }
}

2. Chiama il codice dal tuo progetto esistente.

public static byte[] GeneratePBAReport()
{
    string l_spName = string.Empty;
    string l_reportPath = string.Empty;
    var repCol = new List<ReportDataSource>();
    var repParCol = new ReportParameter[1];

    if (id == "")
    {
        l_reportPath = HttpContext.Current.Server.MapPath("~\\.rdlc");
        l_spName = "";
    }
    else
    {
        l_reportPath = HttpContext.Current.Server.MapPath("~\\.rdlc");
        l_spName = "";
    }

    repParCol[0] = new ReportParameter("pID", "");

    var ds = new DataSet();
    using (var sqlCmd = new SqlCommand(l_spName,
            new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString)))
    {
        sqlCmd.CommandType = CommandType.StoredProcedure;
        var sqlParam = new SqlParameter() { Value = "", ParameterName = "" };
        sqlCmd.Parameters.Add(sqlParam);
        sqlCmd.CommandTimeout = 300;
        using (var sqlAdapter = new SqlDataAdapter(sqlCmd))
        {
            sqlAdapter.Fill(ds);
        }
    }

    var rds = new ReportDataSource();
    rds.Name = "";
    rds.Value = ds.Tables[0];
    repCol.Add(rds);
    rds = new ReportDataSource();
    rds.Name = "";
    rds.Value = ds.Tables[1];
    repCol.Add(rds);
    rds = new ReportDataSource();
    rds.Name = "";
    rds.Value = ds.Tables[2];
    repCol.Add(rds);
    rds = new ReportDataSource();
    rds.Name = "";
    rds.Value = ds.Tables[3];
    repCol.Add(rds);

    string deviceInfo;
    deviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>";

    return NewDomainReport.Render("PDF", deviceInfo, "-", l_reportPath, true,
                                  repCol, string.Empty, new List<string[]>(), repParCol);
}

Per un test davvero rapido, puoi provare ad aggiungere l'impostazione CAS al web.config come descritto nell'articolo.

In un'applicazione ASP.NET puoi usare <trust legacyCasModel="true" level="Full"/> nella sezione system.web del file web.config per ottenere lo stesso risultato.

Se le prestazioni migliorano sensibilmente, il codice qui sopra si comporterà allo stesso modo. Il suo vantaggio è che crea un AppDomain separato anziché incidere sull'intera soluzione.