把DataGrid数据源的查询语句用于数据报表.
http://www.wzjcw.net/vbgood/taishan/index.html有个打印报表的例子.
http://www.wzjcw.net/vbgood/taishan/index.html有个打印报表的例子.
解决方案 »
- VB6一个奇怪的问题
- access是/否数据类型在查询中的处理问题
- 问一个VB ,,Alignment能弄成按一下,标签的文字左对齐,再按一下右对齐,再按一下居中,这样循环下去吗
- 新人求救各位大虾,VB.NET中Richtextbox怎么实现逐行修改!在线等待................
- 北京的朋友来看一下,聘VB程序员!
- 高手请进:关于dir获得文件夹名称的顺序问题, very urgent! 在线等待...
- BlueDog: 我用VB6.0做的双向链表类,请大家测试使用,可以在你的软件中使用
- ADO关于版本问题
- 两条sql语句合并
- 这样是否有错?是否能实现同时打印?请指教!微分相送。
- 用vb写execl时,怎样作到一个cell的左边划线为粗线
- 做一个DLL,里面能否有想析构函数一样的东西对一些私有变量初始话?
http://www.csdn.net/oldexpert/TopicView.asp?id=125441&table=200101
你在Form中间加一个PictureBox,设Appearance=0,大小比例与一张打印纸相似。
再加两个按钮:“预览“(Preview)和“打印“(Print)按钮。你用DataGrid的内容换掉以下的打印内容,自已可以做一个预览和打印功能了:Sub SetData(obj As Object)
If TypeOf obj Is Picture Then
obj.Cls
End If
On Error GoTo exit_data
Dim CX, CY, i, curY, curX
Dim StepRow, lColor, adjRowHeight, iBottomRight
lColor = obj.ForeColor
'首行显示公司名称
CX = 4
obj.Font.Bold = False
obj.Font.Size = 9
obj.CurrentY = CX
obj.CurrentX = obj.Width / 2 - obj.TextWidth("地球")
'obj.ForeColor = RGB(256, 0, 0)
obj.Print "地球公司"
StepRow = obj.TextHeight("A")
'标题
CX = CX + StepRow
obj.Font.Bold = True
obj.Font.Size = 2 * 9
obj.CurrentY = CX
obj.CurrentX = obj.Width / 2 - obj.TextWidth("存款")
obj.ForeColor = RGB(256, 0, 0) '红色
obj.Print "存款单据"
StepRow = obj.TextHeight("A")
'首部信息第一行
obj.ForeColor = lColor '恢复
CX = CX + StepRow
obj.Font.Bold = False
obj.Font.Size = 9
obj.CurrentY = CX: obj.CurrentX = 150
obj.Print "左"
obj.CurrentY = CX: obj.CurrentX = obj.Width / 2 - obj.TextWidth("中") / 2
obj.Print "中"
obj.CurrentY = CX: obj.CurrentX = obj.ScaleWidth - 150 - obj.TextWidth("右")
obj.Print "右"
'=================画行线=================
curY = CX + StepRow: curX = 150
For i = 1 To 5
If i = 1 Then
obj.DrawWidth = 2
Else
obj.DrawWidth = 1
End If
obj.Line (curX, curY)-(obj.ScaleWidth - 150, curY)
iBottomRight = curY
'~~~~~
adjRowHeight = obj.TextHeight("A") + 3
curY = curY + adjRowHeight + 4
Next
'=============画竖线=================
curY = CX + StepRow: curX = 150
For i = 1 To 2
obj.DrawWidth = 1
obj.Line (curX, curY)-(curX, iBottomRight)
curX = curX + obj.Width / 2 - obj.TextWidth("中") / 2
Next
obj.Line (obj.ScaleWidth - 150, curY)-(obj.ScaleWidth - 150, iBottomRight)
'==============打印数据==================
Dim str
str = "打印数据"
curY = CX + StepRow + ((adjRowHeight + 4) / 2) - obj.TextHeight("A") / 2
curX = (150 + obj.Width / 2) / 2 - obj.TextWidth(str) / 2
obj.CurrentY = curY
obj.CurrentX = curX
obj.Print str
'...
Exit Sub
exit_data:
MsgBox Error
Exit Sub
End SubPrivate Sub Preview_Click()
SetData Pic
End SubPrivate Sub Print_Click()
SetData Printer
Printer.EndDoc
End Sub
Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=用户名;Initial Catalog=数据库名;Data Source=服务器名"
Adodc1.RecordSource = "SELECT * FROM table"
Adodc1.Refresh
DataGrid1_Refresh
end sub
private sub command1_click()
RowHeight = 200
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
CommonDialog1.ShowPrinter
For Num = 0 To Adodc1.Recordset.RecordCount - 1
DataGrid1.Row = Num
For Index = 0 To 24
DataGrid1.Col = Index
PrintText DataGrid1.Text, LeftStart + 160 + Lblwidth + Index * 800, TopStart + 1100 + (RowHeight + 200) * (Num + 3.7)
Next
Next
Printer.EndDoc
ErrHandler:
end subSub PrintFont(tName As String, nSize As Integer, bBold As Boolean, bUnderline As Boolean, bItalic As Boolean)
Printer.FontName = tName
Printer.FontSize = nSize
Printer.FontBold = bBold
Printer.FontUnderline = bUnderline
Printer.FontItalic = bItalic
End SubSub PrintText(tText As String, tx As Integer, ty As Integer)
Printer.CurrentX = tx
Printer.CurrentY = ty
Printer.Print tText
End SubSub DataGrid1_Refresh()DataGrid1.Columns(0).Width = 1300
DataGrid1.Columns(1).Width = 1000
DataGrid1.Columns(0).Caption = "文件ID"
DataGrid1.Columns(1).Caption = "分类"
end sub
index代表打印的列