简体   繁体   English

如何在C#中更改SSRS报告的高度?

[英]How to change the height of a SSRS report in C#?

Hi I am working on a MVC Project in which I have a reports page where user can view reports with the use of a report viewer. 嗨,我正在开发一个MVC项目,其中我有一个报告页面,用户可以使用报告查看器查看报告。

I need to set the page size dynamically for a report, I tried many ways to solve this but I couldn't. 我需要为报告动态设置页面大小,我尝试了很多方法来解决这个问题,但我做不到。

I can change the ReportViewer Size with this code 我可以使用此代码更改ReportViewer大小

rptviewer.Height = Unit.Pixel(520);

Kindly help me in with the following queries. 请帮助我解决以下问题。

1.Is it possible to change the SSRS report page height using C# Code? 1.是否可以使用C#代码更改SSRS报告页面高度?

2.Is it possible to change the paper size in the runtime? 2.是否可以在运行时更改纸张尺寸?

My Previous workarounds 我以前的解决方法

<-------- 1 ---------> <-------- 1 --------->

 System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
                pg.Margins.Top = 0;
                pg.Margins.Bottom = 0;
                pg.Margins.Left = 0;
                pg.Margins.Right = 0;
                System.Drawing.Printing.PaperSize size = new PaperSize(); 
                size.RawKind = (int)PaperKind.A5;
                pg.PaperSize = size;  
                rptviewer.SetPageSettings(pg);  
                ViewBag.ReportViewer = rptviewer;
                return View("_ReportView");

<-------- 2 ---------> <-------- 2 --------->

 System.Drawing.Printing.PageSettings MyPageSize= new System.Drawing.Printing.PageSettings(); 
 MyPageSize.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 17, 12); 
 rptviewer.SetPageSettings(MyPageSize);

<-------- 3 ---------> <-------- 3 --------->

var setup = rptviewer.GetPageSettings();           
setup.PaperSize.Height = 1500;
rptviewer.SetPageSettings(setup);

None of the above logic worked for me :-( 上述逻辑都不适合我:-(

The thing is, In order to control the page size in the rendering process we need to pass the proper Device Information Settings to the report at run time. 问题是,为了在渲染过程中控制页面大小,我们需要在运行时将正确的设备信息设置传递给报表。 This will work for physical page oriented renders like PDF, Image, etc. This is a simple Xml string that can be passed as a parameter to the report in order to control these settings. 这适用于面向物理页面的渲染,如PDF,图像等。这是一个简单的Xml字符串,可以作为参数传递给报表,以便控制这些设置。 Each export type has a different set of properties that can be overridden and controlled in this way. 每种导出类型都有一组不同的属性,可以通过这种方式覆盖和控制。

In the following example, an export to PDF is performed and passes the page height and width to match an A4 paper size as the targeted device (line 66): 在以下示例中,执行导出为PDF并传递页面高度和宽度以匹配A4纸张尺寸作为目标设备(第66行):

 private void RenderReportToClient()
 {
     //set credentials
     RSExecuteProxy.ReportExecutionService rs = new RSExecuteProxy.ReportExecutionService();
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

    RSProxy.ReportingService2005 rsInfo = new RSProxy.ReportingService2005();
    rsInfo.Credentials = System.Net.CredentialCache.DefaultCredentials;
     // init render args
    byte[] result = null;
    string reportPath = rptViewer.ServerReport.ReportPath;
   string format = "PDF";
    string historyId = null;
     string encoding;
    string mimeType;
     string extension;
    RSExecuteProxy.Warning[] warnings = null;
     string[] streamIDs = null;
     //init exec info
     RSExecuteProxy.ExecutionInfo execInfo = new RSExecuteProxy.ExecutionInfo();
     RSExecuteProxy.ExecutionHeader execHeader = new RSExecuteProxy.ExecutionHeader();
     rs.ExecutionHeaderValue = execHeader;
     //get report
     execInfo = rs.LoadReport(reportPath, historyId);
     String SessionId = rs.ExecutionHeaderValue.ExecutionID;
    //get parameter info
     ReportParameterInfoCollection parameters = rptViewer.ServerReport.GetParameters();
    //figure out how many parameters we will have 
     //those with multi-value will need there own ParameterValue in the array
    int paramCount = 0;
     foreach (ReportParameterInfo pramInfo in parameters)
     {
         paramCount += pramInfo.Values.Count;
    }

    RSExecuteProxy.ParameterValue[] prams = new SSRSWeb.RSExecuteProxy.ParameterValue[paramCount];
    int currentPramPosition = 0;

     //set pram values
     foreach (ReportParameterInfo pramInfo in parameters)
     {
         foreach (string pramValue in pramInfo.Values)
         {
           prams[currentPramPosition] = new SSRSWeb.RSExecuteProxy.ParameterValue();
            prams[currentPramPosition].Label = pramInfo.Name;
            prams[currentPramPosition].Name = pramInfo.Name;
         prams[currentPramPosition].Value = pramValue;
            currentPramPosition++;
        }
      }

       rs.SetExecutionParameters(prams, "en-US");

      //build the device settings  (A4 8.3 × 11.7)
       string deviceInfo = string.Format("<DeviceInfo><PageHeight>{0}</PageHeight><PageWidth>{1}</PageWidth></DeviceInfo>", "11.7in", "8.3in");

    //get report bytes
     result = rs.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

       Response.ClearContent();
      Response.AppendHeader("Content-Disposition", "inline;filename=report.pdf");
       Response.AppendHeader("content-length", result.Length.ToString());
     Response.ContentType = "application/pdf";
    Response.BinaryWrite(result);
    Response.Flush();
     Response.Close();
   }

When the report is saved you can view the PDF and examine the properties and notice that the page height and width is 8.3in x 11.7in as specified. 保存报告后,您可以查看PDF并检查属性,并注意页面高度和宽度是指定的8.3英寸x 11.7英寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM