这里面有你要的东西:Private Sub cmdPrint_Click()
   Dim objCmd         As New ADODB.Command
   Dim rsResult       As ADODB.Recordset
   Dim strSql         As String
   
   Dim objCmd1         As New ADODB.Command
   Dim rsResult1       As ADODB.Recordset
   Dim strSql1         As String
   
   Dim objCmd2         As New ADODB.Command
   Dim rsResult2       As ADODB.Recordset
   Dim strSql2         As String
   
    Dim intJobno           As Integer
    Dim oExcel As Excel.Application
    Dim oBook As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    Dim oRange As Excel.Range
    Dim i       As Integer
    Dim j       As Integer
    Dim intseqcount     As Integer
    Dim intTotal        As Integer
    Dim intTotal1        As Integer
    
   intJobno = 346
   strSql = "select JOBNO tjobno,DEPDATE,ARVDATE from T_job where JOBNO = " & intJobno & ""
    
   objCmd.CommandType = adCmdText
   objCmd.ActiveConnection = gobjConnection
   objCmd.CommandTimeout = 300
   objCmd.CommandText = strSql
   
   Set rsResult = objCmd.Execute
   Do While Not rsResult.EOF
        Set oExcel = CreateObject("EXCEL.Application")
        Set oBook = oExcel.Workbooks.Add
        Set oSheet = oBook.Worksheets(1)
        
       ' oExcel.Visible = True
        
        oSheet.Cells(1, 1).value = "Job No"
       
       
        oSheet.Cells(1, 3).value = rsResult("tjobno")
        
        
        oSheet.Cells(2, 1).value = "Departure"
       
        oSheet.Cells(2, 3).value = CStr(rsResult("DEPDATE"))
        
        
        oSheet.Cells(3, 1).value = "Arrival"
        
        oSheet.Cells(3, 3).value = CStr(rsResult("ARVDATE"))
        
        
        oSheet.Cells(5, 1).value = "Seq"
        oSheet.Cells(5, 2).value = "To"
        oSheet.Cells(5, 4).value = "No of Pallets"
     
        oSheet.Cells(5, 5).value = "No of Cartons"
    
        oSheet.Cells(5, 7).value = "Estimate Arrival"
    
        oSheet.Cells(5, 8).value = "Departure"
        oSheet.Cells(5, 9).value = "KM"
        oSheet.Cells(5, 10).value = "Actual Arrival"
        
        oSheet.Cells(5, 11).value = "Departure"
        oSheet.Cells(5, 12).value = "KM"
        
        For i = 1 To 12
            oSheet.Cells(5, i).Borders(xlEdgeBottom).Weight = xlThick
        Next
        
        strSql1 = "select * from T_jobRoute where JOBNO='" & rsResult("tjobno") & "'"
    
        objCmd1.CommandType = adCmdText
        objCmd1.ActiveConnection = gobjConnection
        objCmd1.CommandTimeout = 300
        objCmd1.CommandText = strSql1
        
        Set rsResult1 = objCmd1.Execute
        intseqcount = 0
        Do While Not rsResult1.EOF
            intseqcount = intseqcount + 1
            oSheet.Cells(5 + intseqcount, 1).value = rsResult1("SEQNO")
            oSheet.Cells(5 + intseqcount, 2).value = rsResult1("SUPPNM")
            oSheet.Cells(5 + intseqcount, 7).value = CStr(Format(rsResult1("ARVDATE"), "mm:ss"))
           
            oSheet.Cells(5 + intseqcount, 8).value = CStr(Format(rsResult1("DEPDATE"), "mm:ss"))
            
            oSheet.Cells(5 + intseqcount, 9).value = rsResult1("Distance")
            If IsNull(rsResult1("ACTARVDATE")) Then
                oSheet.Cells(5 + intseqcount, 10).value = ""
            Else
                oSheet.Cells(5 + intseqcount, 10).value = CStr(Format(rsResult1("ACTARVDATE"), "mm:ss"))
            End If
                        
            If IsNull(rsResult1("ACTDEPDATE")) Then
               oSheet.Cells(5 + intseqcount, 11).value = ""
             Else
               oSheet.Cells(5 + intseqcount, 11).value = IIf(IsNull(rsResult1("ACTDEPDATE")), " ", CStr(Format(rsResult1("ACTDEPDATE"), "mm:ss")))
            End If
            
            oSheet.Cells(5 + intseqcount, 12).value = rsResult1("Distance")
            rsResult1.MoveNext
        Loop
 
        For i = 3 To 12
         oSheet.Cells(6 + intseqcount, i).Borders(xlEdgeBottom).Weight = xlThin
        Next
        
        oSheet.Cells(7 + intseqcount, 3).value = "Total"
        
        oSheet.Cells(9 + intseqcount, 5).value = "Res"
        oSheet.Cells(9 + intseqcount, 6).value = "HighWay Fee"
        oSheet.Cells(9 + intseqcount, 8).value = "Time"
        oSheet.Cells(9 + intseqcount, 10).value = "Meter"
        oSheet.Cells(9 + intseqcount, 12).value = "Fuel"
 oSheet.PageSetup.Orientation = xlLandscape
   
   oSheet.PrintOut
    
End Sub

解决方案 »

  1.   

    这是我找到的一个。
    如果要例子我可以给你数据库管理系统的开发人员经常感叹的一个问题就是:我们中国人的报表太复杂了!无规则、嵌套、斜线、交叉线等历来都是困挠开发人员的最大问题。设计一个数据库固然有一定的技巧,设计数据操作也固然需要一定的逻辑分析能力,但这些问题对一般的开发人员来说应该是不成为问题的。用户可是不管你采用了多么灵活的算法多么方便的操作,他们最感兴趣的是最后他们出来的报表如何漂亮,出报表的操作如何简单(最好是一个按钮解决所有的问题)。笔者在开发数据库管理系统方面也稍有些经验,从FoxPro、Delphi、PowerBuilder一直到现在用的VB,都遇到过设计打印报表的问题,这些软件在设计报表的过程中可谓各有千秋,我这里不一一细说。我在这里只向大家介绍一种我迄今为止最为满意的一种设计打印报表的方法:利用VB操作EXCEL来生成复杂的报表。 
     一. 用VB创建外部EXCEL对象 
        大多数大型 ActiveX-enabled 应用程序和其它 ActiveX 部件,在它们的对象层次中都提供了一个顶层外部可创建对象。该对象提供了对该层次中其它对象的访问,并且还提供对整个应用程序起作用的方法和属性。 
      例如,每个 Microsoft Office 应用程序提供一个顶层 Application 对象。下面语句显示了对Microsoft Excel的 Application 对象的引用: 
    Dim xlApp As Excel.ApplicationSet xlApp = New Excel.Application---- 然后,可以用这些变量来访问在EXCEL应用程序中的从属对象、以及这些对象的属性和方法。例如: 
    Set xlApp = CreateObject("Excel.Application") ‘激活EXCEL应用程序xlApp.Visible = False ‘隐藏EXCEL应用程序窗口Set xlBook = xlApp.Workbooks.Open(strDestination) ‘打开工作簿,strDestination为一个EXCEL报表文件Set xlSheet = xlBook.Worksheets(1)‘设定工作表---- 二. 用EXCEL 97设计报表的模版文件 
    ---- EXCEL 97是一个非常优秀的创建报表的工具。它提供的单元格任意合并、拆分和绘图功能基本上能够满足设计所有复杂报表的需求。它对任意一个单元格的格式随意控制,更为随心所欲地设计报表提供了强大的支持。 
    ---- 根据用户提供的报表,我们可以很快在EXCEL里生成模版文件。所谓生成模版文件只是为了满足用户多方面的需求而设计的。也是为了适合报表以后的更改而做的一点预备工作。例如用户需要打印几百张职工履历表,但其格式都是一致的,并且随着时间和实际情况的变化,表格格式有可能需要改变,我们设计一个模版文件显然可以“以不变应万变”了。 
    ---- 生成工作表时我们应当记录下要填充内容的单元格编号和该单元格内要填充的数据字段。这样形成一个表格,在写程序时一目了然。如: 
    Cell(4,2) 职工姓名 Cell(6,6) 毕业学校Cell(4,4) 职工性别 Cell(6,7) 所学专业Cell(4,6) 职工民族 Cell(6,9) 工作时间(表一)---- 在程序里我们当然不要对模版文件进行操作了,我们只需要对模版文件的一个拷贝进行操作就行(这也是我们设计模版文件的一个目的和好处)。如下面的例子: 
    Dim strSource, strDestination As StringstrSource = App.Path & "\Excels\RegisterFee.xls" ‘RegisterFee.xls就是一个模版文件strDestination = App.Path & "\Excels\Temp.xls" FileCopy strSource, strDestination ‘将模版文件拷贝到一个临时文件---- 三. 生成工作表内容 
    ---- 有了上述两步工作的铺垫,我们下面接着就只要根据(表一)的格式给各单元格赋值了。如: 
    datPrimaryRS.Recordset.MoveFirst ‘datPrimaryRS为Data控件If IsNull(datPrimaryRS.Recordset!姓名) = False ThenxlSheet.Cells(4, 2) = datPrimaryRS.Recordset!姓名End IfIf IsNull(datPrimaryRS.Recordset!性别) = False ThenxlSheet.Cells(4, 4) = datPrimaryRS.Recordset!性别End IfIf IsNull(datPrimaryRS.Recordset!民族) = False ThenxlSheet.Cells(4, 6) = datPrimaryRS.Recordset!民族End If………………      四. 打印报表 
       生成了工作表后,就可以对EXCEL发出打印指令了。 
        注意在执行打印操作之前应该对EXCEL临时文件执行一次保存操作,以免在退出应用程序后EXCEL还提示用户是否保存已修改的文件,让用户觉得莫名其妙。如下语句: 
    xlBook.Save ‘保存文件xlSheet.PrintOut ‘执行打印xlApp.Quit ‘退出EXCEL   至此读者应该看到,我们设计的报表打印是通过EXCEL程序来后台实现的。用户根本看不到具体过程,他们只看到一张张漂亮的报表轻易地被打印出来了。