Crystal reports 9 的使用示例(一)
2005-01-13 19:43
399 查看
'*****************************************************************
'*crystal report 的使用示例
'*适用版本:9.0
'*制作人:马亚红
'*制作时间:2004年12月28日
'*情况:此为在水晶报表软件中制作报表(rpt文件)的调用方式
'*菜单->工程->引用->选择:
'*1、Crystal Reports 9 ActiveX Desioner Desion and Runtime Library
'*2、Crystal Reports 9 activeX Desioner Run Time Library
'*好多朋友有这个问题:报表显示后数据没有刷新,解决方法如下:
'*1、在报表的设计界面,菜单“文件”--》“选项”--》“建立报表”选项卡--》把“将数据与报表一起保存”前的勾去掉后保存即可
'*2、调用报表对象的DiscardSavedData方法
'*****************************************************************
Option Explicit
Dim Report As New CrystalReport1
Dim wjfilesys As FileSystemObject 'FSO 对象,用于文件操作
Dim WithEvents CrSecRH As Section '报表显示区对象
Dim capp As New CRAXDDRT.Application '报表集合对象
Dim carp As New CRAXDRT.Report '报表集合中的报表对象
Dim carsubp As New CRAXDRT.Report '报表集合对象中的子报表对象
Dim Pic2 As OLEObject '报表对象中的Ole对象
Dim mytxt As CRAXDRT.TextObject '报表对象中的Textbox对象
Private Sub Combo1_Change() '放大(缩小)比例
CRViewer91.Zoom (CInt(Combo1.Text))
End Sub
Private Sub Command1_Click() '显示第一页
CRViewer91.ShowFirstPage
End Sub
Private Sub Command10_Click() '打印报表
CRViewer91.PrintReport
End Sub
Private Sub Command11_Click()
CRViewer91.showclosebutton
End Sub
Private Sub Command2_Click() '刷新报表
CRViewer91.Refresh
End Sub
Private Sub Command3_Click() '显示上一页
CRViewer91.ShowPreviousPage
End Sub
Private Sub Command4_Click() '显示下一页
CRViewer91.ShowNextPage
End Sub
Private Sub Command5_Click() '显示最后一页
CRViewer91.ShowLastPage
End Sub
Private Sub Command6_Click() '在报表内容内搜索
If (txtSearch.Text <> "") Then
CRViewer91.SearchForText (txtSearch.Text)
End If
txtSearch.Text = ""
End Sub
Private Sub Command7_Click() '导入出PDF
Dim myExportFile As String
myExportFile = App.Path + "/temp.pdf"
If wjfilesys.FileExists(myExportFile) Then
wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTPortableDocFormat
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub
Private Sub Command8_Click() '导出到Excel
Dim myExportFile As String
myExportFile = App.Path + "/temp.xls"
If wjfilesys.FileExists(myExportFile) Then
wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTExcel97
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub
Private Sub Command9_Click() '导出到WORD
Dim myExportFile As String
myExportFile = App.Path + "/temp.doc"
If wjfilesys.FileExists(myExportFile) Then
wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTWordForWindows
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub
Private Sub Form_Load()
Screen.MousePointer = vbHourglass
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim i As Integer
If cn.State = adStateOpen Then cn.Close
'SQL SERVER连接方式
With cn
.Provider = "sqloledb"
.ConnectionString = "data source=dd;initial catalog=dfd;user id=sa;password=aaa"
.Open
End With
'Mdb的连接方式
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" + App.Path + "/Test.mdb;Persist Security Info=False"
.Open
End With
strsql = "select * from carsort"
If rs.State = adStateOpen Then rs.Close
With rs
.ActiveConnection = cn
.CursorLocation = adUseClient
.Open strsql, cn, adOpenDynamic, adLockOptimistic
End With
Set carp = capp.OpenReport(App.Path + "/CryStalTest.rpt") '取报表对象
Set carsubp = carp.OpenSubreport("myhgyp") '取子报表对象
Set CrSecRH = carp.Sections("PageHeaderSection1") '取报表头显示区对象
Set Pic2 = carp.Sections("PageHeaderSection1").ReportObjects("picture1") '取报表头显示区中的Ole对象
Set mytxt = CrSecRH.ReportObjects("mytxt")
For i = 1 To carp.Database.Tables.Count '设置报表的数据源
If carp.Database.Tables.Item(i).Name = "carsort" Then
carp.Database.Tables(i).SetDataSource rs
End If
Next
For i = 1 To carsubp.Database.Tables.Count '设置子报表的数据源
If carp.Database.Tables.Item(i).Name = "carsort" Then
carsubp.Database.Tables(i).SetDataSource rs
End If
Next
Screen.MousePointer = vbHourglass
'第一种设置参数的方式
carp.ParameterFields(1).ClearCurrentValueAndRange
carp.ParameterFields(1).AddCurrentValue ("myhgyp")
carp.ParameterFields(2).ClearCurrentValueAndRange
carp.ParameterFields(2).AddCurrentValue (CInt("3"))
'第二种设置参数的方式
carp.ParameterFields.GetItemByName("gg").ClearCurrentValueAndRange
carp.ParameterFields.GetItemByName("gg").AddCurrentValue ("myh11" & vbCrLf & "-2gyp")
carp.ParameterFields.GetItemByName("ff").ClearCurrentValueAndRange
carp.ParameterFields.GetItemByName("ff").AddCurrentValue (CInt("673"))
carp.DiscardSavedData
CRViewer91.ReportSource = carp
CRViewer91.ViewReport
Screen.MousePointer = vbDefault
CRViewer91.Zoom 100
'----------------------
Screen.MousePointer = vbDefault
Set wjfilesys = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub CrSecRH_Format(ByVal pFormattingInfo As Object) '格式化报表头显示区中的对象
Set Pic2.FormattedPicture = LoadPicture(App.Path & "/tsconfig.bmp") 'changes the pic in the Report Header
mytxt.SetText Format(mytxt.Text, "####年##月##日") '格式化TextBox中的文本显示方式
End Sub
Private Sub Form_Resize() '设置报表显示区域
CRViewer91.Top = 0
CRViewer91.Left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth
End Sub
'*crystal report 的使用示例
'*适用版本:9.0
'*制作人:马亚红
'*制作时间:2004年12月28日
'*情况:此为在水晶报表软件中制作报表(rpt文件)的调用方式
'*菜单->工程->引用->选择:
'*1、Crystal Reports 9 ActiveX Desioner Desion and Runtime Library
'*2、Crystal Reports 9 activeX Desioner Run Time Library
'*好多朋友有这个问题:报表显示后数据没有刷新,解决方法如下:
'*1、在报表的设计界面,菜单“文件”--》“选项”--》“建立报表”选项卡--》把“将数据与报表一起保存”前的勾去掉后保存即可
'*2、调用报表对象的DiscardSavedData方法
'*****************************************************************
Option Explicit
Dim Report As New CrystalReport1
Dim wjfilesys As FileSystemObject 'FSO 对象,用于文件操作
Dim WithEvents CrSecRH As Section '报表显示区对象
Dim capp As New CRAXDDRT.Application '报表集合对象
Dim carp As New CRAXDRT.Report '报表集合中的报表对象
Dim carsubp As New CRAXDRT.Report '报表集合对象中的子报表对象
Dim Pic2 As OLEObject '报表对象中的Ole对象
Dim mytxt As CRAXDRT.TextObject '报表对象中的Textbox对象
Private Sub Combo1_Change() '放大(缩小)比例
CRViewer91.Zoom (CInt(Combo1.Text))
End Sub
Private Sub Command1_Click() '显示第一页
CRViewer91.ShowFirstPage
End Sub
Private Sub Command10_Click() '打印报表
CRViewer91.PrintReport
End Sub
Private Sub Command11_Click()
CRViewer91.showclosebutton
End Sub
Private Sub Command2_Click() '刷新报表
CRViewer91.Refresh
End Sub
Private Sub Command3_Click() '显示上一页
CRViewer91.ShowPreviousPage
End Sub
Private Sub Command4_Click() '显示下一页
CRViewer91.ShowNextPage
End Sub
Private Sub Command5_Click() '显示最后一页
CRViewer91.ShowLastPage
End Sub
Private Sub Command6_Click() '在报表内容内搜索
If (txtSearch.Text <> "") Then
CRViewer91.SearchForText (txtSearch.Text)
End If
txtSearch.Text = ""
End Sub
Private Sub Command7_Click() '导入出PDF
Dim myExportFile As String
myExportFile = App.Path + "/temp.pdf"
If wjfilesys.FileExists(myExportFile) Then
wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTPortableDocFormat
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub
Private Sub Command8_Click() '导出到Excel
Dim myExportFile As String
myExportFile = App.Path + "/temp.xls"
If wjfilesys.FileExists(myExportFile) Then
wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTExcel97
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub
Private Sub Command9_Click() '导出到WORD
Dim myExportFile As String
myExportFile = App.Path + "/temp.doc"
If wjfilesys.FileExists(myExportFile) Then
wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTWordForWindows
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub
Private Sub Form_Load()
Screen.MousePointer = vbHourglass
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim i As Integer
If cn.State = adStateOpen Then cn.Close
'SQL SERVER连接方式
With cn
.Provider = "sqloledb"
.ConnectionString = "data source=dd;initial catalog=dfd;user id=sa;password=aaa"
.Open
End With
'Mdb的连接方式
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" + App.Path + "/Test.mdb;Persist Security Info=False"
.Open
End With
strsql = "select * from carsort"
If rs.State = adStateOpen Then rs.Close
With rs
.ActiveConnection = cn
.CursorLocation = adUseClient
.Open strsql, cn, adOpenDynamic, adLockOptimistic
End With
Set carp = capp.OpenReport(App.Path + "/CryStalTest.rpt") '取报表对象
Set carsubp = carp.OpenSubreport("myhgyp") '取子报表对象
Set CrSecRH = carp.Sections("PageHeaderSection1") '取报表头显示区对象
Set Pic2 = carp.Sections("PageHeaderSection1").ReportObjects("picture1") '取报表头显示区中的Ole对象
Set mytxt = CrSecRH.ReportObjects("mytxt")
For i = 1 To carp.Database.Tables.Count '设置报表的数据源
If carp.Database.Tables.Item(i).Name = "carsort" Then
carp.Database.Tables(i).SetDataSource rs
End If
Next
For i = 1 To carsubp.Database.Tables.Count '设置子报表的数据源
If carp.Database.Tables.Item(i).Name = "carsort" Then
carsubp.Database.Tables(i).SetDataSource rs
End If
Next
Screen.MousePointer = vbHourglass
'第一种设置参数的方式
carp.ParameterFields(1).ClearCurrentValueAndRange
carp.ParameterFields(1).AddCurrentValue ("myhgyp")
carp.ParameterFields(2).ClearCurrentValueAndRange
carp.ParameterFields(2).AddCurrentValue (CInt("3"))
'第二种设置参数的方式
carp.ParameterFields.GetItemByName("gg").ClearCurrentValueAndRange
carp.ParameterFields.GetItemByName("gg").AddCurrentValue ("myh11" & vbCrLf & "-2gyp")
carp.ParameterFields.GetItemByName("ff").ClearCurrentValueAndRange
carp.ParameterFields.GetItemByName("ff").AddCurrentValue (CInt("673"))
carp.DiscardSavedData
CRViewer91.ReportSource = carp
CRViewer91.ViewReport
Screen.MousePointer = vbDefault
CRViewer91.Zoom 100
'----------------------
Screen.MousePointer = vbDefault
Set wjfilesys = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub CrSecRH_Format(ByVal pFormattingInfo As Object) '格式化报表头显示区中的对象
Set Pic2.FormattedPicture = LoadPicture(App.Path & "/tsconfig.bmp") 'changes the pic in the Report Header
mytxt.SetText Format(mytxt.Text, "####年##月##日") '格式化TextBox中的文本显示方式
End Sub
Private Sub Form_Resize() '设置报表显示区域
CRViewer91.Top = 0
CRViewer91.Left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth
End Sub
相关文章推荐
- 使用C#调用存储过程,用函数合理组织代码,使程序更加的清晰(示例)
- 使用C语言打造通讯录管理系统和教学安排系统的代码示例
- ss命令使用示例
- 微信小程序使用audio组件播放音乐功能示例【附源码下载】
- java使用Hex编码解码实现Aes加密解密功能示例
- showModelDialog弹出文件下载窗口的使用示例
- Python3.X 线程中信号量的使用方法示例
- ICE简单介绍及使用示例
- android 中使用TableLayout实现表单布局效果示例
- 【转】Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例--不错
- 使用Docker部署Spring Boot的应用示例
- TP(thinkPHP)框架多层控制器和多级控制器的使用示例
- Java使用正则表达式获取子文本的方法示例
- matplotlib基本使用方法示例
- Spring使用入门示例
- android Binder的使用方式 示例
- php中session使用示例
- c#中abstract、override、new、virtual、sealed使用和示例
- zlib库剖析(3):使用示例zpipe.c
- JDOM使用示例