Gok*_*aha 5 c# sql-server reporting-services ssrs-2008 ssrs-2012
嗨,我正在开发一个MVC项目,其中我有一个报告页面,用户可以使用报告查看器查看报告.
我需要为报告动态设置页面大小,我尝试了很多方法来解决这个问题,但我做不到.
我可以使用此代码更改ReportViewer大小
rptviewer.Height = Unit.Pixel(520);
Run Code Online (Sandbox Code Playgroud)
请帮助我解决以下问题.
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");
Run Code Online (Sandbox Code Playgroud)
<-------- 2 --------->
System.Drawing.Printing.PageSettings MyPageSize= new System.Drawing.Printing.PageSettings();
MyPageSize.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 17, 12);
rptviewer.SetPageSettings(MyPageSize);
Run Code Online (Sandbox Code Playgroud)
<-------- 3 --------->
var setup = rptviewer.GetPageSettings();
setup.PaperSize.Height = 1500;
rptviewer.SetPageSettings(setup);
Run Code Online (Sandbox Code Playgroud)
上述逻辑都不适合我:-(
问题是,为了控制渲染过程中的页面大小,我们需要在运行时将正确的设备信息设置传递给报表。这适用于面向物理页面的渲染,如 PDF、图像等。这是一个简单的 Xml 字符串,可以作为参数传递到报表以控制这些设置。每种导出类型都有一组不同的属性,可以通过这种方式覆盖和控制这些属性。
\n\n在以下示例中,执行导出为 PDF 并传递页面高度和宽度以匹配 A4 纸张尺寸作为目标设备(第 66 行):
\n\n private void RenderReportToClient()\n {\n //set credentials\n RSExecuteProxy.ReportExecutionService rs = new RSExecuteProxy.ReportExecutionService();\n rs.Credentials = System.Net.CredentialCache.DefaultCredentials;\n\n RSProxy.ReportingService2005 rsInfo = new RSProxy.ReportingService2005();\n rsInfo.Credentials = System.Net.CredentialCache.DefaultCredentials;\n // init render args\n byte[] result = null;\n string reportPath = rptViewer.ServerReport.ReportPath;\n string format = "PDF";\n string historyId = null;\n string encoding;\n string mimeType;\n string extension;\n RSExecuteProxy.Warning[] warnings = null;\n string[] streamIDs = null;\n //init exec info\n RSExecuteProxy.ExecutionInfo execInfo = new RSExecuteProxy.ExecutionInfo();\n RSExecuteProxy.ExecutionHeader execHeader = new RSExecuteProxy.ExecutionHeader();\n rs.ExecutionHeaderValue = execHeader;\n //get report\n execInfo = rs.LoadReport(reportPath, historyId);\n String SessionId = rs.ExecutionHeaderValue.ExecutionID;\n //get parameter info\n ReportParameterInfoCollection parameters = rptViewer.ServerReport.GetParameters();\n //figure out how many parameters we will have \n //those with multi-value will need there own ParameterValue in the array\n int paramCount = 0;\n foreach (ReportParameterInfo pramInfo in parameters)\n {\n paramCount += pramInfo.Values.Count;\n }\n\n RSExecuteProxy.ParameterValue[] prams = new SSRSWeb.RSExecuteProxy.ParameterValue[paramCount];\n int currentPramPosition = 0;\n\n //set pram values\n foreach (ReportParameterInfo pramInfo in parameters)\n {\n foreach (string pramValue in pramInfo.Values)\n {\n prams[currentPramPosition] = new SSRSWeb.RSExecuteProxy.ParameterValue();\n prams[currentPramPosition].Label = pramInfo.Name;\n prams[currentPramPosition].Name = pramInfo.Name;\n prams[currentPramPosition].Value = pramValue;\n currentPramPosition++;\n }\n }\n\n rs.SetExecutionParameters(prams, "en-US");\n\n //build the device settings (A4 8.3 \xc3\x97 11.7)\n string deviceInfo = string.Format("<DeviceInfo><PageHeight>{0}</PageHeight><PageWidth>{1}</PageWidth></DeviceInfo>", "11.7in", "8.3in");\n\n //get report bytes\n result = rs.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);\n\n Response.ClearContent();\n Response.AppendHeader("Content-Disposition", "inline;filename=report.pdf");\n Response.AppendHeader("content-length", result.Length.ToString());\n Response.ContentType = "application/pdf";\n Response.BinaryWrite(result);\n Response.Flush();\n Response.Close();\n }\nRun Code Online (Sandbox Code Playgroud)\n\n保存报告后,您可以查看 PDF 并检查属性,并注意到页面高度和宽度为指定的 8.3 英寸 x 11.7 英寸。
\n