C# 本地报告rdlc到pdf的速度非常慢

C# 本地报告rdlc到pdf的速度非常慢,c#,rdlc,C#,Rdlc,是否有任何方法可以提高本地报告的性能,如果没有,甚至还有其他方法?下面是将rdlc转换为pdf的当前代码。我一直在寻找一个解决方案已经有一段时间了,但普遍的共识似乎是,这只是缓慢的,谢谢你的帮助 public byte[] genReportBytes(int id, string fromm, string too, string filetype) { reportDetails repD = new reportDetails(); repD = g

是否有任何方法可以提高本地报告的性能,如果没有,甚至还有其他方法?下面是将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;
}
public byte[]genReportBytes(int-id,string-fromm,string-too,string-filetype)
{
reportDetails repD=新的reportDetails();
repD=getOneReport(id);
LocalReport=新的LocalReport();
if(fromm!=null)
repD.parameters命令行=“@startdate=“+fromm;
如果(太多!=null)
repD.parameters命令行+=“@enddate=“+too;
字符串RDLCPath=ConfigurationManager.AppSettings[“RDLCPath”];
字符串ReportOutputPath=ConfigurationManager.AppSettings[“ReportOutputPath”];
字符串RDLCName=repD.RDLCName;
RDLCPath+=@“\”+RDLCName;
report.ReportPath=RDLCPath;
字符串sqlGet=repD.SQLOfReport;
report.DataSources.Add(新的ReportDataSource(repD.DatasetName,getReportData(sqlGet,repD.ParametersCommandLine));
//导出到字节数组
警告[]警告;
字符串[]流线;
字符串模拟类型;
字符串编码;
字符串文件名扩展名;
字符串deviceInf=“”;
字节[]字节;
字符串扩展;
如果(文件类型=“pdf”)
{
deviceInf=“8.5in11in0in0in”;
//fileName=ReportOutputPath+@“\”+repD.NameOfOutputPDF+“.PDF”;
bytes=report.Render(“pdf”),deviceInf,out mimeType,out encoding,out filenameExtension,
输出流线,输出警告);
}
其他的
{
//fileName=ReportOutputPath+@“\”+repD.NameOfOutputPDF+“.XLS”;
字节=report.Render(
“Excel”,空,输出mimeType,输出编码,
外接分机,
输出流线,输出警告);
}
返回字节;
}
我在这里发布了一个答案

基本上,您必须在单独的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);
    }
}
私有静态字节[]呈现(string reportRenderFormat、string deviceInfo、string DisplayName、string ReportPath、bool Visible、ReportDataSourceCollection数据源、string repMainContent、List RepSubcent、ReportParameter[]reportParam)
{
AppDomainSetup=new AppDomainSetup{ApplicationBase=Environment.CurrentDirectory,LoaderOptimization=LoaderOptimization.MultiDomainHost};
setup.SetCompatibilitySwitches(新[]{“NetFx40_LegacySecurityPolicy”});
AppDomain _casPolicyEnabledDomain=AppDomain.CreateDomain(“完全信任”,null,setup);
尝试
{
WebReportviewer.FullTrustReportviewer rvNextgenReport2=(WebReportviewer.FullTrustReportviewer)\ u casPolicyEnabledDomain.CreateInstanceFromAndUnwrap(typeof(WebReportviewer.FullTrustReportviewer.Assembly.CodeBase,typeof(WebReportviewer.FullTrustReportviewer.FullName);
rvNextgenReport2.初始化(显示名称、报告路径、可见、报告参数、报告渲染格式、设备信息、报告内容、报告分包);
foreach(ReportDataSource(数据源中的ReportDataSource)
{
rvNextgenReport2.AddDataSources(_ReportDataSource.Name,(DataTable)_ReportDataSource.Value);
}
返回rvNextgenReport2.Render(reportRenderFormat,deviceInfo);
}
最后
{
卸载(_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);
            }
        }
    }
}
命名空间WebReportviewer
{
[可序列化]
公共类FullTrustReportviewer:MarshallByRefObject
{
私人信托;
公共FullTrustReportviewer()
{
FullTrust=new ReportViewer();
FullTrust.ShowExportControls=false;
FullTrust.ShowPrintButton=true;
FullTrust.ShowZoomControl=true;
FullTrust.SizeToReportContent=false;
FullTrust.ShowReportBody=true;
FullTrust.ShowDocumentMapButton=false;
FullTrust.showFindControl=true;
FullTrust.LocalReport.SubreportProcessing+=LocalReport\u SubreportProcessing;
}
public void Initialize(字符串DisplayName、字符串ReportPath、bool Visible、报告参数[]reportParam、字符串reprenderformat、字符串deviceInfo、字符串repMainContent、列表repsubcent)
{
FullTrust.LocalReport.DisplayName=DisplayName;
FullTrust.LocalReport.ReportPath=ReportPath;
FullTrust.Visible=可见;
FullTrust.LocalReport.LoadReportDefinition(新的StringReader(repMainContent));
FullTrust.LocalReport.SetParameters(reportParam);
repsubcent.ForEach(x=>
{
FullTrust.LocalReport.LoadSubreportDefinition(x[0],新的StringReader(x[1]);
});
FullTrust.LocalReport.DataSources.Clear();
}       
公共字节[]呈现(string reportRenderFormat,string deviceInfo)
{
返回FullTrust.LocalReport.Render(reportRenderFormat,deviceInfo);
}
public void AddDataSources(字符串p,DataTable)
{
Add(newreportdatasource(p,datatable));
}
public SubreportProcessingEventHandler SubreportProcessing{get;set;}
public static void LocalReport_SubreportProcessing(对象发送者,SubreportProcessingEventArgs e)
{
LocalReport lr=(LocalReport)发送方;
e、 DataSources.Clear();
报告数据源rds;
if(如ReportPath.Contains(“DataTable2”))
{
DataTable dt=(DataTable)lr.DataSources[“DataTable2”].Value;
数据视图dv=新数据视图(dt);
dv.RowFilter=string.Format(“Id={0}”,e.Parameters[“Id”].Values[0]);
rds=新的ReportDataSource(“Da