VB怎么做报表和打印?

今天给朋友做一个管理系统,需要做个报表和打印...我不知道怎么做,知道的请告知,... 今天给朋友做一个管理系统, 需要做个报表和打印...我不知道怎么做,知道的请告知, 展开
 我来答
w11662386
推荐于2017-10-03
知道答主
回答量:35
采纳率:0%
帮助的人:23.4万
展开全部
这是我写的一个例子,Ddh是查询用的,你可以不要,如果数据多的话,考虑分页
调用:
call caigoudd(printer,"0001") '打印
也可以用图片框显示
call caigoudd(picture1,"0001") '显示

Sub CaiGoudd(Obj As Object, ByVal Ddh As String) '打印采购单
Dim Shang As Integer, Zhuo As Integer '边距
Dim Cangmin As String, Addr As String, Tel As String, Fax As String
Dim Hen As Single, Su As Single, Hh As Single, Hh1 As Single, Beisu As Single, Ss As Single, mm As Single
Dim Gongfa As String
Dim i As Integer, nn As Integer, Rsnum As Integer

Dim Rs As New ADODB.Recordset '采购信息
Dim RS1 As New ADODB.Recordset '供方信息
Dim sqlStr As String

Set Cn = New ADODB.Connection
Cn.CursorLocation = adUseClient
Cn.Open cnStr
sqlStr = "select * from cgjl where 订单号码 like '" & Ddh & "'"
Rs.Open sqlStr, Cn, adOpenStatic, adLockReadOnly
If Rs.EOF Then
MsgBox "订单号码不存在,请教对!", , "注意"
Rs.Close
Cn.Close
Exit Sub
End If
Rsnum = Rs.RecordCount
If Rsnum > 6 Then
MsgBox "采购物品大于6条,请用不同的订单号。", , "注意"
Rs.Close
Cn.Close
Exit Sub
End If

sqlStr = "select * from gfxx where 供方简称 like '" & Rs.Fields("供方名称") & "'"
RS1.Open sqlStr, Cn '供方资料
If RS1.EOF Then
MsgBox "供方信息不存在,请添加!", , "注意"
Rs.Close
RS1.Close
Cn.Close
Exit Sub
End If

Ddh = "NO: " & Ddh
Cangmin = "电器有限公司"
Addr = "浙江省"
Tel = "222222222"
Fax = "222222222"

With Obj
.ScaleMode = 6 '度量单位为mm

mm = .ScaleWidth / 210 '210是A4纸的宽度,mm是显示倍数
Shang = 25 * mm
Zhuo = 15 * mm
nn = 12 * mm
Beisu = (Obj.ScaleWidth - 2 * Zhuo) / nn

.DrawWidth = IIf(TypeName(Obj) = "Printer", 7, 1) '线宽度
.FontName = "楷体_GB2312"
.FontSize = 20 * mm
'打印表头
Hen = .ScaleWidth
Su = .ScaleHeight
.CurrentX = (.ScaleWidth - .TextWidth(Cangmin)) / 2 '居中
.CurrentY = Shang
Obj.Print Cangmin
.CurrentX = (.ScaleWidth - .TextWidth("采购订单")) / 2
.CurrentY = .CurrentY + 3 * mm
Obj.Print "采购订单"
.FontName = "宋体"
.FontSize = 14 * mm
.CurrentY = .CurrentY + 3 * mm
.CurrentX = Zhuo
Obj.Print " RHF7.4-07A";
.CurrentX = Hen - .TextWidth(Ddh) - Zhuo - 3 * mm
Obj.Print Ddh
'画表格
Hh = Zhuo '表格左上角坐标
Ss = .CurrentY + 3 * mm

For i = 0 To 9
Obj.Line (Hh, Ss + i * nn)-(Hen - Zhuo, Ss + i * nn) '横线
Next
Obj.Line (Hh, Ss + 12.5 * nn)-(Hen - Zhuo, Ss + 12.5 * nn)
Obj.Line (Hh, Ss + 18 * nn)-(Hen - Zhuo, Ss + 18 * nn)

Obj.Line (Hh, Ss)-(Hh, Ss + 18 * nn) '竖线
Obj.Line (Hen - Zhuo, Ss)-(Hen - Zhuo, Ss + 18 * nn)
Obj.Line (Hh + nn, Ss + 2 * nn)-(Hh + nn, Ss + 9 * nn)
Obj.Line (Hh + 3.5 * nn, Ss)-(Hh + 3.5 * nn, Ss + 9 * nn)
Obj.Line (Hh + 7 * nn, Ss + 2 * nn)-(Hh + 7 * nn, Ss + 9 * nn)
Obj.Line (Hh + 8 * nn, Ss + 2 * nn)-(Hh + 8 * nn, Ss + 9 * nn)
Obj.Line (Hh + 9.5 * nn, Ss + 2 * nn)-(Hh + 9.5 * nn, Ss + 9 * nn)
Obj.Line (Hh + 11.5 * nn, Ss + 2 * nn)-(Hh + 11.5 * nn, Ss + 9 * nn)
Obj.Line (Hen / 2, Ss + 12.5 * nn)-(Hen / 2, Ss + 18 * nn)
'填数据
Hh1 = (nn - .TextHeight("供")) / 2
.CurrentX = Hh + (3.5 * nn - .TextWidth("供方单位")) / 2
.CurrentY = Ss + Hh1
Obj.Print "供方单位";
.CurrentX = Hh + 3.5 * nn + ((Beisu - 3.5) * nn - .TextWidth(RS1.Fields("供方全称") & "")) / 2
Obj.Print RS1.Fields("供方全称") & ""
.CurrentX = Hh + (3.5 * nn - .TextWidth("传真号码")) / 2
.CurrentY = Ss + nn + Hh1
Obj.Print "传真号码";
.CurrentX = Hh + 3.5 * nn + ((Beisu - 3.5) * nn - .TextWidth(RS1.Fields("传真") & "")) / 2
Obj.Print RS1.Fields("传真") & ""

.CurrentX = Hh + (nn - .TextWidth("序号")) / 2
.CurrentY = Ss + 2 * nn + Hh1
Obj.Print "序号";
.CurrentX = Hh + nn + (2.5 * nn - .TextWidth("品 名")) / 2
Obj.Print "品 名";
.CurrentX = Hh + 3.5 * nn + (3.5 * nn - .TextWidth("规格型号")) / 2
Obj.Print "型号规格";
.CurrentX = Hh + 7 * nn + (nn - .TextWidth("单位")) / 2
Obj.Print "单位";
.CurrentX = Hh + 8 * nn + (1.5 * nn - .TextWidth("数量")) / 2
Obj.Print "数量";
.CurrentX = Hh + 9.5 * nn + (2 * nn - .TextWidth("交货日期")) / 2
Obj.Print "交货日期";
.CurrentX = Hh + 11.5 * nn + ((Beisu - 11.5) * nn - .TextWidth("备 注")) / 2
Obj.Print "备 注"

.FontSize = 12 * mm
Hh1 = (nn - .TextHeight("供")) / 2
For i = 1 To Rsnum
.CurrentX = Hh + (nn - .TextWidth(Format(i))) / 2
.CurrentY = Ss + (2 + i) * nn + Hh1
Obj.Print Format(i);
.CurrentX = Hh + nn + (2.5 * nn - .TextWidth(Rs.Fields("产品名称"))) / 2
Obj.Print Rs.Fields("产品名称");
.CurrentX = Hh + 3.5 * nn + (3.5 * nn - .TextWidth(Rs.Fields("规格型号"))) / 2
Obj.Print Rs.Fields("规格型号");
.CurrentX = Hh + 7 * nn + (nn - .TextWidth(Rs.Fields("单位"))) / 2
Obj.Print Rs.Fields("单位");
.CurrentX = Hh + 8 * nn + (1.5 * nn - .TextWidth(Format(Rs.Fields("采购数量")))) / 2
Obj.Print Format(Rs.Fields("采购数量"));
.CurrentX = Hh + 9.5 * nn + (2 * nn - .TextWidth(Rs.Fields("交货日期"))) / 2
Obj.Print Rs.Fields("交货日期");
.CurrentX = Hh + 11.5 * nn + ((Beisu - 11.5) * nn - .TextWidth(Rs.Fields("备注"))) / 2
Obj.Print Rs.Fields("备注")

Rs.MoveNext
Next
Rs.Close
RS1.Close
Cn.Close

.CurrentY = Ss + 9 * nn
Obj.Print
.CurrentX = Hh + 3 * mm
Obj.Print "注:"
Obj.Print
.CurrentX = Hh + 3 * mm
Obj.Print "1. 因供方产品质量问题引起的客户索赔及未按时交货引起的经济损失由供方承担。"
Obj.Print
.CurrentX = Hh + 3 * mm
Obj.Print "2. 接到采购单后请速盖章回签,如两天内不回签视同交货期默认。"
Obj.Print
.CurrentX = Hh + 3 * mm
Obj.Print "3. 价格:"

.FontSize = 14 * mm

.CurrentY = Ss + 12.5 * nn
Obj.Print
.CurrentX = Hh + 3 * mm
Obj.Print "需方单位(章)";
.CurrentX = .ScaleWidth / 2 + 3 * mm
Obj.Print "供方单位(章)"
Obj.Print

.CurrentX = Hh + 3 * mm
Obj.Print " "; Cangmin
.CurrentY = .CurrentY + 0.5 * .TextHeight("A")
.CurrentX = Hh + 3 * mm
.FontSize = 12 * mm
Obj.Print "地 址:"; Left(Addr, 11)
.CurrentY = .CurrentY + 0.5 * .TextHeight("A")
.CurrentX = Hh + 3 * mm
Obj.Print " "; Right(Addr, 8)
.CurrentY = .CurrentY + 0.5 * .TextHeight("A")
.CurrentX = Hh + 3 * mm
Obj.Print "电 话:"; Tel
.CurrentY = .CurrentY + 0.5 * .TextHeight("A")
.CurrentX = Hh + 3 * mm
Obj.Print "传 真:"; Fax
.CurrentY = .CurrentY + 0.5 * .TextHeight("A")
.CurrentX = Hh + 3 * mm
Obj.Print "联系人:"
Obj.Print

.CurrentX = .ScaleWidth / 2 - .TextWidth(Format(Date, "yyyy年mm月dd日 "))
Obj.Print Format(Date, "yyyy年mm月dd日");
.CurrentX = .ScaleWidth - Zhuo - .TextWidth("年 月 日 ")
Obj.Print "年 月 日"

If TypeName(Obj) = "Printer" Then
.EndDoc
End If

End With

End Sub
有手艺的农民
2012-02-22 · TA获得超过1610个赞
知道小有建树答主
回答量:1191
采纳率:33%
帮助的人:733万
展开全部
最简单的方法:先用 Excel做报表,然后导入access(最好设置密码),利用access中的转换工具将报表转换成97版(我用的是access2003版本,没转换的话涉及到vb6.0sp6)。然后在vb中添加一个data和一个dbgrid以及数个command和text,利用data添加报表,利用dbgrid显示报表,利用text的DataSource 属性来添加和更改报表,最后是调用excel来作报表预览和打印(唉......知识有限,我就知道这么多了)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式