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

时间:2018-03-16 13:30:40

标签: c# sql-server reporting-services ssrs-2008 ssrs-2012

您好我正在开发一个MVC项目,其中我有一个报告页面,用户可以使用report viewer.

查看报告

我需要为报告动态设置页面大小,我尝试了很多方法来解决这个问题,但我做不到。

我可以使用此代码更改ReportViewer大小

rptviewer.Height = Unit.Pixel(520);

请帮我处理以下问题。

1.是否可以使用C#代码更改SSRS报告页面高度?

2.是否可以在运行时更改纸张尺寸?

我之前的变通办法

< -------- 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 --------->

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

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

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

以上逻辑都不适合我: - (

1 个答案:

答案 0 :(得分:0)

问题是,为了在呈现过程中控制页面大小,我们需要在运行时将适当的Device Information Settings传递给报表。这将适用于面向页面的物理渲染,例如PDF,Image等。这是一个简单的Xml字符串,可以将其作为参数传递给报表,以控制这些设置。每个导出类型都有一组不同的属性,可以通过这种方式覆盖和控制这些属性。

在以下示例中,将导出为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();
   }

保存报告后,您可以查看PDF并检查属性,并注意页面的高度和宽度为8.3英寸x 11.7英寸(指定)。