以下是用activereport作的通用报表代码: Option Explicit Private m_grid As MSHFlexGrid Private bDone As Boolean Dim iLeft As Integer 'pstr为报表标题Property Set Grid(grd As MSHFlexGrid) Dim ctl As Object Dim I As Integer Dim IleftLab As Integer Dim iTopplace As Integer '表头最上端横线位置 Dim iTopheight As Integer '表头高度 iTopplace = 800 iTopheight = 400 Me.Caption = Pstr Set m_grid = grd Set ctl = Detail.Controls.Add("ddactivereports.line") '加表格细节最左边竖线 With ctl .X1 = 0: .Y1 = 0: .X2 = 0: .Tag = 400: iLeft = iLeft + 40 PrintWidth = iLeft End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '加表头最左边竖线 With ctl .X1 = 0: .Y1 = 800: .X2 = 0: .Y2 = iTopplace + iTopheight: IleftLab = IleftLab + 40 End With
For I = 0 To m_grid.Cols(0) - 1 '加表格内容 If m_grid.ColWidth(I) > 150 Then '如小于150则不添加 '表格明细 Set ctl = Detail.Controls.Add("DDActiveReports.Field") With ctl .Left = iLeft: .Top = 80: .Width = m_grid.ColWidth(I, 0): .Tag = I Fields.Add .Name .DataField = .Name .Font.Name = "宋体" iLeft = iLeft + .Width + 40 End With
Set ctl = Detail.Controls.Add("ddactivereports.line") '加表格竖线 With ctl .X1 = iLeft: .Y1 = 0: .X2 = iLeft: .Tag = 401 + I iLeft = iLeft + 40 PrintWidth = iLeft End With '表头 Set ctl = PageHeader.Controls.Add("DDActiveReports.label") With ctl .Left = IleftLab: .Top = iTopplace + 100: .Width = m_grid.ColWidth(I, 0): .Height = iTopheight .Caption = m_grid.TextMatrix(0, I) .Font.Name = "宋体" IleftLab = IleftLab + .Width + 40 End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '加表格竖线 With ctl .X1 = IleftLab: .Y1 = iTopplace: .X2 = IleftLab: .Y2 = iTopplace + iTopheight IleftLab = IleftLab + 40 End With End If Next I '表头 Set ctl = PageHeader.Controls.Add("ddactivereports.label") '报表标题 With ctl .Caption = Pstr: .Left = 0: .Width = iLeft: .Alignment = 2 .Font.Name = "宋体": .Font.Size = 14: .Height = 400: .Top = 0 End With Set ctl = PageHeader.Controls.Add("ddactivereports.label") '报表打印时间 With ctl .Caption = "制表日期:" & Format(Now, "long date"): .Left = iLeft - 3500: .Width = 3500 .Font.Name = "宋体": .Font.Size = 12: .Height = 300: .Top = 450 End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '表头最上端横线 With ctl .X1 = 0: .Y1 = iTopplace: .Y2 = iTopplace: .X2 = iLeft - 40 End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '表头下端横线 With ctl .X1 = 0: .Y1 = iTopplace + iTopheight: .Y2 = iTopplace + iTopheight: .X2 = iLeft - 40 End With PageHeader.Height = iTopplace + iTopheight + 2 '表明细 Set ctl = Detail.Controls.Add("ddactivereports.line") '加表格细节中下端横线 With ctl .X1 = 0: .Y1 = Detail.Height: .Y2 = Detail.Height: .X2 = iLeft - 40: .Tag = 301 End With '页脚 Set ctl = PageFooter.Controls.Add("ddactivereports.label") '报表当前页页号 With ctl .Name = "CurPage": .Left = iLeft - 1350 - 700 .Width = 700: .Top = 80: .Alignment = 1 '右对齐 End With
Set ctl = PageFooter.Controls.Add("ddactivereports.field") '报表页"/" With ctl .Text = "/": .Left = iLeft - 1220 .Top = 80: .Width = 220 End With
Set ctl = PageFooter.Controls.Add("ddactivereports.field") '报表总页数 With ctl .SummaryType = ddSMPageCount .SummaryRunning = 0 .SummaryFunc = 0 .Alignment = 0 '左对齐 .Width = 700: .Left = iLeft - 1000: .Top = 80 End With End PropertyPrivate Sub ActiveReport_FetchData(eof As Boolean) Static iRow As Integer Dim ctl As Object Dim I As Integer With m_grid If iRow < .Rows - 1 Then For Each ctl In Detail.Controls If ctl.Tag < 300 And ctl.Tag <> "" Then Fields(ctl.Name).Value = .TextMatrix(iRow + 1, ctl.Tag) End If Next iRow = iRow + 1 eof = False End If End With End SubPrivate Sub ActiveReport_Initialize() Me.Printer.Orientation = ddOLandscape Me.Printer.RenderMode = 1 Me.Zoom = -2 Me.Printer.SetupDialog Me.hWnd End SubPrivate Sub ActiveReport_PageStart() PageFooter.Controls.Item("CurPage") = Me.pageNumber End SubPrivate Sub Detail_BeforePrint() Dim ctl As Object For Each ctl In Detail.Controls If ctl.Tag > 399 And ctl.Tag < 500 Then '为表格竖线 ctl.Y2 = Detail.Height ElseIf ctl.Tag = 301 Then '表格中横线 ctl.Y2 = Detail.Height - 6 ctl.Y1 = Detail.Height - 6 End If Next End Sub
Option Explicit
Private m_grid As MSHFlexGrid
Private bDone As Boolean
Dim iLeft As Integer
'pstr为报表标题Property Set Grid(grd As MSHFlexGrid)
Dim ctl As Object
Dim I As Integer
Dim IleftLab As Integer
Dim iTopplace As Integer '表头最上端横线位置
Dim iTopheight As Integer '表头高度
iTopplace = 800
iTopheight = 400
Me.Caption = Pstr
Set m_grid = grd
Set ctl = Detail.Controls.Add("ddactivereports.line") '加表格细节最左边竖线
With ctl
.X1 = 0: .Y1 = 0: .X2 = 0: .Tag = 400: iLeft = iLeft + 40
PrintWidth = iLeft
End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '加表头最左边竖线
With ctl
.X1 = 0: .Y1 = 800: .X2 = 0: .Y2 = iTopplace + iTopheight: IleftLab = IleftLab + 40
End With
For I = 0 To m_grid.Cols(0) - 1 '加表格内容
If m_grid.ColWidth(I) > 150 Then '如小于150则不添加
'表格明细
Set ctl = Detail.Controls.Add("DDActiveReports.Field")
With ctl
.Left = iLeft: .Top = 80: .Width = m_grid.ColWidth(I, 0): .Tag = I
Fields.Add .Name
.DataField = .Name
.Font.Name = "宋体"
iLeft = iLeft + .Width + 40
End With
Set ctl = Detail.Controls.Add("ddactivereports.line") '加表格竖线
With ctl
.X1 = iLeft: .Y1 = 0: .X2 = iLeft: .Tag = 401 + I
iLeft = iLeft + 40
PrintWidth = iLeft
End With
'表头
Set ctl = PageHeader.Controls.Add("DDActiveReports.label")
With ctl
.Left = IleftLab: .Top = iTopplace + 100: .Width = m_grid.ColWidth(I, 0): .Height = iTopheight
.Caption = m_grid.TextMatrix(0, I)
.Font.Name = "宋体"
IleftLab = IleftLab + .Width + 40
End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '加表格竖线
With ctl
.X1 = IleftLab: .Y1 = iTopplace: .X2 = IleftLab: .Y2 = iTopplace + iTopheight
IleftLab = IleftLab + 40
End With
End If
Next I
'表头
Set ctl = PageHeader.Controls.Add("ddactivereports.label") '报表标题
With ctl
.Caption = Pstr: .Left = 0: .Width = iLeft: .Alignment = 2
.Font.Name = "宋体": .Font.Size = 14: .Height = 400: .Top = 0
End With
Set ctl = PageHeader.Controls.Add("ddactivereports.label") '报表打印时间
With ctl
.Caption = "制表日期:" & Format(Now, "long date"): .Left = iLeft - 3500: .Width = 3500
.Font.Name = "宋体": .Font.Size = 12: .Height = 300: .Top = 450
End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '表头最上端横线
With ctl
.X1 = 0: .Y1 = iTopplace: .Y2 = iTopplace: .X2 = iLeft - 40
End With
Set ctl = PageHeader.Controls.Add("ddactivereports.line") '表头下端横线
With ctl
.X1 = 0: .Y1 = iTopplace + iTopheight: .Y2 = iTopplace + iTopheight: .X2 = iLeft - 40
End With
PageHeader.Height = iTopplace + iTopheight + 2
'表明细
Set ctl = Detail.Controls.Add("ddactivereports.line") '加表格细节中下端横线
With ctl
.X1 = 0: .Y1 = Detail.Height: .Y2 = Detail.Height: .X2 = iLeft - 40: .Tag = 301
End With
'页脚
Set ctl = PageFooter.Controls.Add("ddactivereports.label") '报表当前页页号
With ctl
.Name = "CurPage": .Left = iLeft - 1350 - 700
.Width = 700: .Top = 80: .Alignment = 1 '右对齐
End With
Set ctl = PageFooter.Controls.Add("ddactivereports.field") '报表页"/"
With ctl
.Text = "/": .Left = iLeft - 1220
.Top = 80: .Width = 220
End With
Set ctl = PageFooter.Controls.Add("ddactivereports.field") '报表总页数
With ctl
.SummaryType = ddSMPageCount
.SummaryRunning = 0
.SummaryFunc = 0
.Alignment = 0 '左对齐
.Width = 700: .Left = iLeft - 1000: .Top = 80
End With
End PropertyPrivate Sub ActiveReport_FetchData(eof As Boolean)
Static iRow As Integer
Dim ctl As Object
Dim I As Integer
With m_grid
If iRow < .Rows - 1 Then
For Each ctl In Detail.Controls
If ctl.Tag < 300 And ctl.Tag <> "" Then
Fields(ctl.Name).Value = .TextMatrix(iRow + 1, ctl.Tag)
End If
Next
iRow = iRow + 1
eof = False
End If
End With
End SubPrivate Sub ActiveReport_Initialize()
Me.Printer.Orientation = ddOLandscape
Me.Printer.RenderMode = 1
Me.Zoom = -2
Me.Printer.SetupDialog Me.hWnd
End SubPrivate Sub ActiveReport_PageStart()
PageFooter.Controls.Item("CurPage") = Me.pageNumber
End SubPrivate Sub Detail_BeforePrint()
Dim ctl As Object
For Each ctl In Detail.Controls
If ctl.Tag > 399 And ctl.Tag < 500 Then '为表格竖线
ctl.Y2 = Detail.Height
ElseIf ctl.Tag = 301 Then '表格中横线
ctl.Y2 = Detail.Height - 6
ctl.Y1 = Detail.Height - 6
End If
Next
End Sub
http://www.wzjcw.net/vbgood/taishan/index.html有代码可以参考