[所见非所得2]控制水晶报表的部分元素不打印
在前文<[所见非所得]使用参数控制水晶报表的部分元素不打印>中,使用了参数结合编程的方法实现。
CSDN论坛上的网友提供了另外一种更好的方法
http://topic.csdn.net/u/20081103/08/4aa67c3a-185b-4f96-8197-1fda53e6c0aa.html?seed=1144679551
方法重点:显示与打印分离,程序中直接设置对象的抑制显示属性。然后打印。
核心语句:
myReport.ReportDefinition.ReportObjects["Text1"].ObjectFormat.EnableSuppress=true;
模板中不再需要使用参数和公式,直接操作即可。以 WinForm 为例,核心代码为
Public Class Form2
Private myReport As New ReportDocument
Private Sub ConfigureCrystalReports()
Dim reportPath As String = Application.StartupPath & "\" & "crystalreport2.rpt"
myReport.Load(reportPath)
CrystalReportViewer1.ReportSource = myReport
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConfigureCrystalReports()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myReport.ReportDefinition.ReportObjects("Text8").ObjectFormat.EnableSuppress = True
myReport.ReportDefinition.ReportObjects("Subreport1").ObjectFormat.EnableSuppress = True
myReport.PrintOptions.PrinterName = "Microsoft Office Document Image Writer"
myReport.PrintToPrinter(1, False, 1, 99)
End Sub
End Class
Private myReport As New ReportDocument
Private Sub ConfigureCrystalReports()
Dim reportPath As String = Application.StartupPath & "\" & "crystalreport2.rpt"
myReport.Load(reportPath)
CrystalReportViewer1.ReportSource = myReport
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConfigureCrystalReports()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myReport.ReportDefinition.ReportObjects("Text8").ObjectFormat.EnableSuppress = True
myReport.ReportDefinition.ReportObjects("Subreport1").ObjectFormat.EnableSuppress = True
myReport.PrintOptions.PrinterName = "Microsoft Office Document Image Writer"
myReport.PrintToPrinter(1, False, 1, 99)
End Sub
End Class