注册 登录
编程论坛 VB6论坛

VB如何控制水晶报表

nong_hua 发布于 2007-12-13 11:26, 5957 次点击
VB如何控制水晶报表?包括打印纸张的自定义、或者只打印查询出来的指定数据。那位知道的给个例子,在这里先谢了。
18 回复
#2
purana2007-12-13 12:29
这是我昨天写的一个简单的处理水晶报表的类.
可以参考一下.
Option Explicit
Private str_FileName As String
Private m_Report As CRAXDRT.Report
Private m_Application As CRAXDDRT.Application
Private objReportViewer As CRViewer9

Public Function ExportFiles(byte_Type As Byte) As Boolean
    If Dir(str_FileName) <> "" Then
        Kill str_FileName
    End If
   
    On Error GoTo errHandle
   
    With m_Report
        .ExportOptions.DiskFileName = str_FileName
        .ExportOptions.DestinationType = crEDTDiskFile
        If byte_Type = 1 Then    '导出PDF
            .ExportOptions.FormatType = crEFTPortableDocFormat
            .ExportOptions.PDFExportAllPages = True
        ElseIf byte_Type = 2 Then  '导出Excel
            .ExportOptions.FormatType = crEFTExcel97
            .ExportOptions.ExcelExportAllPages = True
        ElseIf byte_Type = 3 Then  '导出Word
            .ExportOptions.FormatType = crEFTWordForWindows
            .ExportOptions.WORDWExportAllPages = True
        End If
        .Export (False)
    End With
    ExportFiles = True
    Exit Function
   
errHandle:
    ExportFiles = False
End Function

Public Property Let SetReport(ReportFile As String)
    Set m_Report = m_Application.OpenReport(ReportFile)
End Property

Public Property Let SetDatabaseSource(Con As ADODB.Connection)
    m_Report.Database.SetDataSource Con
End Property

Public Sub SetTableReportSource(tbName As String, Rs As ADODB.Recordset)
    Dim i As Integer
    For i = 1 To m_Report.Database.Tables.Count
        If m_Report.Database.Tables.Item(i).Name = tbName Then
            m_Report.Database.Tables(i).SetDataSource Rs
        End If
    Next
End Sub

Public Sub ShowReport()
    objReportViewer.ReportSource = m_Report
    objReportViewer.ViewReport
    objReportViewer.Zoom 100
End Sub

'设定文件名
Public Property Let FileName(s_Filename As String)
    str_FileName = s_Filename
End Property

'设置报表控件
Public Property Let ReportViewer(rViewer As CRViewer9)
    Set objReportViewer = rViewer
End Property

'打印报表
Public Sub PrintReport()
    objReportViewer.PrintReport
End Sub

Public Sub ShowNextPage()
    objReportViewer.ShowNextPage
End Sub

Public Sub ShowFirstPage()
    objReportViewer.ShowFirstPage
End Sub

Public Sub ShowPreviousPage()
    objReportViewer.ShowPreviousPage
End Sub

Public Sub ShowLastPage()
    objReportViewer.ShowLastPage
End Sub

Private Sub Class_Initialize()
    Set m_Application = New CRAXDDRT.Application
End Sub

Private Sub Class_Terminate()
    On Error Resume Next
   
    Set m_Report = Nothing
    Set m_Application = Nothing
End Sub
#3
nong_hua2007-12-13 14:45
先谢谢了。不过不知道怎样试。
#4
nong_hua2007-12-13 15:10
类我看不懂啊,兄弟帮帮忙嘛。
#5
purana2007-12-13 15:28
Option Explicit
Dim objReport As ClsReport
Dim Con As ADODB.Connection
Dim Rs As ADODB.Recordset

Private Sub Form_Load()
    Set Con = New ADODB.Connection
    Con.Open "Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:\Northwind.mdb"
    Set Rs = New ADODB.Recordset
    Rs.CursorLocation = adUseClient
    Rs.Open "Select OrderID,CustomerID,EmployeeID from Orders where EmployeeID=5", Con, adOpenKeyset, adLockReadOnly, adCmdText
   
    Set objReport = New ClsReport
    With objReport
        .SetReport = "d:\Report1.rpt"
        .ReportViewer = CRViewer91
        .SetDatabaseSource = Con
        .SetTableReportSource "Orders", Rs
        .ShowReport
    End With
End Sub

Private Sub Form_Resize()
    With CRViewer91
        .Left = 0
        .Top = 0
        .Width = ScaleWidth
        .Height = ScaleHeight
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Rs = Nothing
    Set Con = Nothing
End Sub

Private Sub mnuExcel_Click()
    Dim s_Filename As String
    s_Filename = GetFileName()
    If s_Filename = "" Then Exit Sub
    objReport.FileName = s_Filename
    If objReport.ExportFiles(2) Then
        MsgBox "导出成功"
    Else
        MsgBox "导出失败"
    End If
End Sub

Private Sub mnuPDF_Click()
    Dim s_Filename As String
    s_Filename = GetFileName()
    If s_Filename = "" Then Exit Sub
    objReport.FileName = s_Filename
    If objReport.ExportFiles(1) Then
        MsgBox "导出成功"
    Else
        MsgBox "导出失败"
    End If
End Sub

Private Sub mnuPrint_Click()
    objReport.PrintReport
End Sub

Private Function GetFileName() As String
    On Error GoTo errHandle
   
    With CommonDialog1
        .CancelError = True
        .Filter = "PDF Files(*.pdf)|*.pdf|Excel Files(*.xls)|*.xls|Word Files(*.doc)|*.doc"
        .ShowSave
        GetFileName = .FileName
    End With
    Exit Function
errHandle:
    GetFileName = ""
End Function

Private Sub mnuQuit_Click()
    Unload Me
End Sub

Private Sub mnuWord_Click()
    Dim s_Filename As String
    s_Filename = GetFileName()
    If s_Filename = "" Then Exit Sub
    objReport.FileName = s_Filename
    If objReport.ExportFiles(3) Then
        MsgBox "导出成功"
    Else
        MsgBox "导出失败"
    End If
End Sub
#6
nong_hua2007-12-13 16:42
大哥帮个忙,我想从数据库中查询出来的数据自动生成报表(使用水晶报表)如何实现?你给我类我看不懂,还有别的简单点的办法吗?谢谢!
#7
purana2007-12-13 17:19
只有本站会员才能查看附件,请 登录
#8
wen06602008-09-02 14:15
Thank you
#9
wen06602008-09-02 14:16
谢谢 Purana
#10
seremban2008-10-13 05:49
代码我下了,但是我是crystal 11, 打开你的工程的时候报告错误
报告'crviewer9.dll' could not be loaded',如何找到这个crviewer9.dll呢
我是vb新手,也刚用crystal report
1:如何找到这个crviewer9.dll? 我在crystal11的安装目录下搜索crviewer什么都没找到
2:我想用crystal 11, 里面对应的控件是不是crviewer11.dll呢?
谢谢啦
  yinxiaohu@
#11
三断笛2008-10-13 20:14
回复 1# nong_hua 的帖子
水晶报表有很多控制打印的API还有实例,但无论如何,不过要控制只打印哪几行估计是做不到,要控制打印多少页,第几页,这些都没问题
#12
三断笛2008-10-13 20:19
回复 5# purana 的帖子
最简单的:
先用水晶报表连接数据库,把字段列表拉过来,然后把字段拉报表上,接着就在VB里执行查询,把报表里的字段都传进去就好了
#13
三断笛2008-10-13 20:21
回复 10# seremban 的帖子
水晶报表就是有这缺点,高版本不兼容低版本,光凭这点就给给水晶报表打个不及格

你要重新引用,把所有CRYSTAL9组件改成11的
#14
jixinyu42010-03-19 13:42
dffffffffffffff
sssssssssssssssssssssssssss
#15
yywd42011-05-28 23:18
下载学习,谢谢了。
#16
zhm198406222012-08-16 16:20
看看!!!!!!!!!!!!!!!!!!!!
#17
adffdda2016-03-29 21:34
谢谢分享!!
#18
adffdda2016-03-29 21:34
谢谢分享!!
#19
adffdda2016-03-29 21:35
谢谢分享!!
1