c# - 本地报告rdlc转pdf很慢

标签 c# rdlc

是否有任何方法可以提高本地报告的性能,如果没有,甚至还有其他选择?将 rdlc 转换为 pdf 的当前代码如下。一段时间以来一直在寻找解决方案,但普遍的共识似乎是它的速度很慢,感谢您的帮助。

  public byte[] genReportBytes(int id, string fromm, string too, string           filetype)
{
    reportDetails repD = new reportDetails();
    repD = getOneReport(id);

    LocalReport report = new LocalReport();

    if (fromm != null)
        repD.ParametersCommandLine = "@startdate=" + fromm;

    if (too != null)
        repD.ParametersCommandLine += " @enddate=" + too;

    string RDLCPath = ConfigurationManager.AppSettings["RDLCPath"];
    string ReportOutputPath = ConfigurationManager.AppSettings["ReportOutputPath"];

    string RDLCName = repD.RDLCName;
    RDLCPath += @"\" + RDLCName;
    report.ReportPath = RDLCPath;

    string sqlGet = repD.SQLOfReport;

    report.DataSources.Add(new ReportDataSource(repD.DatasetName, getReportData(sqlGet, repD.ParametersCommandLine)));

    // export to byte array

    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string filenameExtension;
    string deviceInf = "";
    byte[] bytes;
    string extension;

    if (filetype == "pdf")
    {
        deviceInf = "<DeviceInfo><PageHeight>8.5in</PageHeight><PageWidth>11in</PageWidth><MarginLeft>0in</MarginLeft><MarginRight>0in</MarginRight></DeviceInfo>";
        //fileName = ReportOutputPath + @"\" + repD.NameOfOutputPDF + ".PDF";
        bytes = report.Render("pdf", deviceInf, out mimeType, out encoding, out filenameExtension,
        out streamids, out warnings);
    }
    else
    {
        //fileName = ReportOutputPath + @"\" + repD.NameOfOutputPDF + ".XLS";
        bytes = report.Render(
              "Excel", null, out mimeType, out encoding,
               out extension,
              out streamids, out warnings);
    }

    return bytes;
}

最佳答案

我在这里发布了一个答案 slow-performance-with-dynamic-grouping-and-reportviewer-in-local-mode

基本上你必须在一个单独的 Appdomain 中运行 reportviewer,这是 Render 方法,它从你当前的 reportviewer 控件中获取所有参数。

private static byte[] Render(string reportRenderFormat, string deviceInfo, string DisplayName, string ReportPath, bool Visible, ReportDataSourceCollection DataSources, string repMainContent, List<string[]> repSubContent, ReportParameter[] reportParam)
{
    AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory, LoaderOptimization = LoaderOptimization.MultiDomainHost };
    setup.SetCompatibilitySwitches(new[] { "NetFx40_LegacySecurityPolicy" });
    AppDomain _casPolicyEnabledDomain = AppDomain.CreateDomain("Full Trust", null, setup);
    try
    {
        WebReportviewer.FullTrustReportviewer rvNextgenReport2 = (WebReportviewer.FullTrustReportviewer)_casPolicyEnabledDomain.CreateInstanceFromAndUnwrap(typeof(WebReportviewer.FullTrustReportviewer).Assembly.CodeBase, typeof(WebReportviewer.FullTrustReportviewer).FullName);
        rvNextgenReport2.Initialize(DisplayName, ReportPath, Visible, reportParam, reportRenderFormat, deviceInfo, repMainContent, repSubContent);

        foreach (ReportDataSource _ReportDataSource in DataSources)
        {
            rvNextgenReport2.AddDataSources(_ReportDataSource.Name, (DataTable)_ReportDataSource.Value);
        }

        return rvNextgenReport2.Render(reportRenderFormat, deviceInfo);
    }
    finally
    {
        AppDomain.Unload(_casPolicyEnabledDomain);
    }
}

这是将运行报告的新程序集:

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;
           FullTrust.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;               
        }

        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.Visible = Visible;
            FullTrust.LocalReport.LoadReportDefinition(new StringReader(repMainContent)); 
            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);
            }
        }
    }
}

这样做需要对您当前的代码进行最少的更改。 问候。

关于c# - 本地报告rdlc转pdf很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30511856/

相关文章:

c# - 创建列数可变的 rdlc 报告

c# - 如何重新加载注册表设置? (Symbol 设备上的紧凑框架)

C#:在不相关的类之间共享属性、事件和方法

c# - 如何合并多个pdf文件(在运行时生成)?

RDLC 分组行

.net - 将.NET类用作带有SSRS RDLC的数据源

reporting-services - RDLC 表达式扩展方法

c# - 使用 c# 的输出作为 MATLAB 的输入

c# - 将数据分组到单列并合并另一列

c# - 在数据表中搜索值