重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这个专业我也学得不是很好,用书上得例子吧。
在达坂城等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、做网站 网站设计制作按需网站制作,公司网站建设,企业网站建设,品牌网站制作,成都全网营销,外贸营销网站建设,达坂城网站建设费用合理。
某公司尽力需要了解公司在全国各地得销售情况,设计一个水晶报表。统计个地区得销售总额和全国总额
准备工作
用 SQL server 2000 创建一个数据库company 在该数据库中创建一个表Sales.用于储存区域销售数据
在表Sales 中添加若干条记录
设计报表
打开 Visual Studio.NET创建名为 Salesreport得 WINDOWS应用程序 将Form1.cs 名更改为 frmSalesReport.cs
frmSalesReport窗体得属性设置里。 吧 frmSalesReport.cs得Text属性更改为"区域销售报表"
把NAME属性更改为frmSalesReport。
在 VisualStudio.NET 菜单中选择 项目添加新项。在"添加新项"窗口得“类别”区域扩展 “本地项目项”,选择数据,在"模板"区域选择"数据集" 在"名称"那将Dataset1.xsd改名 SalesDetaset.xsd
单击"打开" 将生成一个新得结构文件 SalesDataset.xsd 该文件将显示在 ADO.NET数据集设计器中。
这是我写的一个例子,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
核心代码:Requery方法!
也可以直接使用ADODB.Recordset!不使用DataEnvironment!例:
...
public adoRecordsetX as ADODB.Recordset
...
Private Sub DataReport_Initialize()
adoRecordsetX.Requery
Set DataReport1.DataSource = adoRecordsetX '设置 DataReport 的数据源
...
'绑定某字段
Me.Sections.Item("Section1").Controls.Item("text1").DataField=adoRecordsetX.Fields.Item(0).Name
...
End Sub
wuxiaoqiang的意见:
在你们有关上述问题的回答,我想再增加一种更简单的解决办法。
1.在datareport 报表中设置datamember和datasource为空。
2.将以前绑定的RptTextBox的datamember设为空,保留原来的datafield.
3.在打印操作中增加以下代码:
Dim db As Connection
Set db = New Connection
db.CursorLocation = adUseClient
db.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=" + App.path + "\finance.mdb" + ";"
Set tb = New Recordset
tb.Open "select * from dailyfeeprint", db, adOpenStatic, adLockOptimistic
With tb
.AddNew
![工号] = Combo1(0).Text
![姓名] = txtFields(0).Text
![交费日期] = txtFields(1).Text
.........
.Update
End With
Set data1.Recordset = tb
Set datareport1.DataSource = data1
datareport1.Show
datareport1.PrintReport
在VB中,右键点击右面工程视图中的工程图标,点选其中的添加-Data
Environment(
即建立一个数据环境),再点添加-Data
Report
(即建立一个报表)。一般来说VB自带的报表已能满足使用要求,如果需要功能更加强大的报表控件,可以选择水晶报表控件CrystalReport或TrueDBGrid。
CrystalReportViewer
dataset 定义数据源
.rpt 文件描述具体报表样式
再加一个调用报表的FORM,就可以实现简单的报表功能
Option Explicit
dim Report as New Cystal1
Private Sub Form_Load()
Screen.MousePointer = vbHourglass
'调用水晶报表时置鼠标为沙漏状 CRViewer91.ReportSource = Report '该语句的赋值将在后面被修改
CRViewer91.ViewReport
Screen.MousePointer = vbDefault '调用水晶报表完成后置鼠标为默认形状
End Sub
Private Sub Form_Resize()
CRViewer91.Top = 0
CRViewer91.Left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth
End Sub