在VB中怎么利用一个命令按钮打印(datagrid1)数据表里的数据啊!

解决方案 »

  1.   

    实际上要对所绑定的记录集打印。1 自己编写代码,利用 Printer 对象打印。
    2 利用水晶报表等第三方控件打印
    3 导出到 Excel, Word 等软件中打印。
      

  2.   

    '利用 Word 打印的例子Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oRange As Word.Range
    Dim sTemp, sHeadline, sTitle As String
    Dim i As Integer
    Dim oldRec As LongOn Error GoTo eh
    If adc_card.Recordset.RecordCount = 0 Then MsgBox "没有可打印的数据!", vbCritical: Exit Sub
    oldRec = adc_card.Recordset.AbsolutePosition' Create an instance of Word
    Set oWord = CreateObject("Word.Application")
    ' Add a new, blank document
    Set oDoc = oWord.Documents.add
    oDoc.PageSetup.Orientation = wdOrientLandscape' Get the current document's range object
    Set oRange = oDoc.Range
    sTitle = Format(DTPicker1.value, "yyyy年mm月dd日") & Combo1 & IIf(Len(Text4), "(", "") & Text4 & IIf(Len(Text4), ")", "") & "消费记录明细表" & vbCrLf & vbCrLf
    oRange.Text = sTitleoRange.SetRange Len(sTitle) + 1, Len(sTitle) + 1
    adc_card.Recordset.MoveFirst
    sTemp = adc_card.Recordset.GetString(adClipString, -1, vbTab)
    For i = 0 To Datagrid1.Columns.Count - 1
    sHeadline = sHeadline & Datagrid1.Columns(i).Caption & vbTab
    Next i
    sHeadline = Left(sHeadline, Len(sHeadline) - 1) & vbCrLf
    ' Insert a heading on the string
    sTemp = sHeadline & sTemp
    ' Insert the data into the Word document
    oRange.Text = sTemp
    ' Convert the text to a table and format the table
    oRange.ConvertToTable vbTab, , , , 36oRange.SetRange oRange.End + 1, oRange.End + 1
    oRange.Text = vbCrLf & "制表人:" & AdminName & vbTab & vbTab _
        & "制表日期:" & Format(Date, "long date")
    oRange.ParagraphFormat.Alignment = wdAlignParagraphRightoDoc.Tables(1).Select
    With oWord.Selection
    .Cells.HeightRule = wdRowHeightAtLeast
    .Cells.Height = 16
    .Cells.VerticalAlignment = wdCellAlignVerticalCenter
    End WithoDoc.Tables(1).Columns(2).Width = 40
    oDoc.Tables(1).Columns(8).Width = oDoc.Tables(1).Columns(8).Width + 50
    For i = 2 To adc_card.Recordset.RecordCount + 1
    oDoc.Tables(1).Cell(i, 4).Select
    oWord.Selection.Text = Format(Val(oWord.Selection.Text), "currency")
    Next i
    oDoc.Tables(1).Cell(1, 4).Select
    oWord.Selection.SelectColumn
    oWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
    oDoc.Tables(1).Cell(1, 4).Select
    oWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    oWord.Selection.HomeKey Unit:=wdStory'Formating the Title
    oRange.SetRange 0, Len(sTitle) - 4
    With oRange
    .Font.Name = "宋体"
    .Font.size = 16
    .Font.Bold = True
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    End With' Show Word to the user
    oWord.Visible = TrueIf oldRec >= 1 Then
    adc_card.Recordset.MoveFirst
    adc_card.Recordset.Move oldRec - 1
    End If'CheckBox on form says "立即打印"
    If Check1 Then oWord.PrintOut
      

  3.   

    导到html 
    Private Sub ExportToHtm(ByVal iFileName As String,mrstTable as adodb.recordset)
       Dim lintCount As Integer
       Dim hFile As Long
       Dim lblnFirst As Boolean
       Dim lstrString As String
       
       On Error GoTo ErrHandling
       Let lblnFirst = True
       
       Screen.MousePointer = vbHourglass
       If Not mrstTable Is Nothing Then
          If Not mrstTable.EOF Then
             Let hFile = FreeFile
             Open iFileName For Output As #hFile
             Print #hFile, "<HTML><BODY><TABLE BORDER=1><TR>"
             For lintCount = 0 To mrstTable.Fields.Count - 1
                 Print #hFile, "<TD>" & mrstTable.Fields(lintCount).Name & "</TD>"
             Next
             
             Print #hFile, "</TR>"
             mrstTable.MoveFirst
             With mrstTable
                While Not mrstTable.EOF
                   DoEvents
                   Print #hFile, "<TR>"
                   For lintCount = 0 To .Fields.Count - 1
                       lstrString = IIf(IsNull(.Fields(lintCount).Value), "&nbsp;", .Fields(lintCount).Value)
                       If MyTrim(lstrString) = "" Then lstrString = "&nbsp;"
                       Print #hFile, "<TD>" & lstrString & "</TD>"
                   Next lintCount
                   Print #hFile, "</TR>"
                   mrstTable.MoveNext
                Wend
             End With
             Print #hFile, "</TABLE></BODY></HTML>"
          End If
       End If
    ErrHandling:
       Close #hFile
       Screen.MousePointer = vbDefault
       If ERR.Number <> 0 Then
          MsgBox ERR.Number & vbCrLf & ERR.Description, vbExclamation, Title  'syee
       End If
    End Sub
      

  4.   

    功能特点:  
    1、强大快速的自动分页技术;
    2、页面元素、布局高度集成,自定义;
    3、实现报表无需报表设计器,所有操作由程序控制;
    4、灵活的弹性接口设计;
    5、完善的打印预览和打印操作;
    6、组件性能高效、轻量,可用于VB,VC,DELPHI,C++ Builder,PB,ASP,JSP,.NET等开发环境;
     
    http://www.wave12.com