当前位置:编程学习 > VB >>

如何打印Msflexgrid 里面的数据

如何直接把Msflexgrid里面的数据打印出来,而不是先导出到Excel再打印的,请各位高人帮忙,非常急着用,对Msflexgrid不是很熟悉 用printer来实现
搜索“vb printer”学习吧 参考

基于VB-PRINTER对象的动态报表打印 printer  导出或不导出都可以实现打印,看你的需要吧,不导出写打印部分比较繁琐。
http://topic.csdn.net/u/20100621/15/02a34bdf-88bd-41c6-9305-953d0115e6b8.html
关于不分栏只分页的相对简单很多,参考一下即可 打印窗体,打印Msflexgrid控件,打印记录集都可以
最好还是用打印控件吧,第三方的,有很多,只要传个记录集给控件就可以了,很好用,在客户端都可以设计 还是后台输出到Excel在打印比较爽,格式控制方便啊 '下面是打印方法

Private Sub Command1_Click()
    Printer.ScaleMode = vbCentimeters
    Printer.Font.Size = 24
    Printer.Font.Name = "华文行楷"
    Printer.Print String(16, " ") & "这个是标题"

    Printer.Font.Size = 12
    Printer.Font.Name = "宋体"
    Printer.Print "内容1"
    Printer.Print "内容2"
    Printer.Print "内容3"
    Printer.EndDoc
End Sub



'下面是VsFlexgrid导出Excel,显示所见的表格,你可以借鉴
Sub PrintExcel()                                '打印到Excel
    Dim newxls As New Excel.Application
    Dim newbook As New Excel.Workbook
    Dim newsheet As New Excel.Worksheet
    
    Set newxls = Excel.Application
    Set newbook = newxls.Workbooks.Add
    Set newsheet = newbook.Worksheets(1)
    
    With newxls
        .Visible = True
    End With
    
    newsheet.Cells.VerticalAlignment = xlTop
    newsheet.Columns(3).WrapText = True
    
    With newsheet.PageSetup
        .LeftMargin = newxls.InchesToPoints(0.2)
        .RightMargin = newxls.InchesToPoints(0.2)
        .TopMargin = newxls.InchesToPoints(0.3)
        .BottomMargin = newxls.InchesToPoints(0.2)
        .CenterHorizontally = True
    End With
    
    newsheet.Cells.Font.Size = 12

    newsheet.Cells(1, 1) = "这个是标题"
    newsheet.Cells(1, 1).Font.Name = "黑体"
    newsheet.Cells(1, 1).Font.Size = 24
    newsheet.Cells(1, 1).HorizontalAlignment = xlCenter
    
    newsheet.Cells(2, 1) = StatusBar1.Panels(1).Text
    
    TotalRow = VSFlexGrid1.Rows - 1
    TotalCol = VSFlexGrid1.Cols - 1
    ColCount = 0
    ColLast = 0
    
    For j = 1 To TotalCol
        If VSFlexGrid1.ColWidth(j) <> 0 Then
            ColCount = ColCount + 1
        End If
    Next
    
    For i = 0 To TotalRow
        RowN = 3 + i
        For j = 1 To TotalCol
            If VSFlexGrid1.ColWidth(j) <> 0 Then
                If ColLast >= ColCount Then
                    ColN = 1
                Else
                    ColN = ColLast + 1
                End If
                newsheet.Cells(RowN, ColN).HorizontalAlignment = xlCenter
                newsheet.Cells(RowN, ColN) = VSFlexGrid1.TextMatrix(i, j)
                ColLast = ColN
            End If
        Next
    Next
    
    newsheet.Range(Cells(1, 1), Cells(1, ColCount)).Merge
    newsheet.Range(Cells(2, 1), Cells(2, ColCount)).Merge
    newsheet.Range(Cells(3, 1), Cells(3, ColCount)).Interior.Color = &HFFFF&
    
    With newsheet.Range(Cells(3, 1), Cells((3 + VSFlexGrid1.Rows - 1), ColCount)).Borders
        .LineStyle = 1
        .Weight = 2
    End With
    
    For j = 1 To ColCount
        newsheet.Columns(j).AutoFit
    Next

    Set newsheet = Nothing
    Set newbook = Nothing
    Set newxls = Nothing
End Sub 都是自己写的东西,如果好用就给分吧~
现在急需分,呵
补充:VB ,  控件
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,