VB利用DataReport做报表详解.doc_第1页
VB利用DataReport做报表详解.doc_第2页
VB利用DataReport做报表详解.doc_第3页
VB利用DataReport做报表详解.doc_第4页
VB利用DataReport做报表详解.doc_第5页
已阅读5页,还剩41页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VB利用DataReport做报表首先介绍一下DataReport对象的几个常用属性。一是DataSource,用于设置一个数据源,通过该数据源,数据使用者被绑定到一个数据库;二是DataMember,从DataSource提供的几个数据成员中设置一个特定的数据成员;三是LeftMargin、RightMargin、TopMargin、BottomMargin等,用于指定报表的左右上下的页边距;四是Sections,即DataReport的报表标头、页标头、细节、页脚注、报表脚注5个区域,如果加上分组(可以有多层分组),则增加一对区域,即分组标头、分组脚注。其中DataSource一般是一个数据环境或是ADODB.Connection类型的变量,而DataMember则对应数据环境中的Command或是ADODB.RecordSet类型的变量,推荐使用数据环境及Command,页边界大家肯定都很清楚,下面我主要介绍以下Sections,这也是DataReport的精髓所在。 Sections是一个集合,您可以为每一个Section指定名称,也可以用其缺省的索引,从上到下依次为1、2。每个Section均有Height和Visible属性,您可以在一定条件下使一个Section不可见。在Section中可以放置各种报表控件,其中RptLabel、RptImage、RptShape和RptLine可以放在任意的Section中,用于输出各种文字、图形及表格线;RptTextBox只能放在细节中,一般用于绑定输出DataMemeber提供的数据字段;RptFunction只能被放置在分组注脚中,用于输出使用各种内置函数计算出的合计、最大值、最小值、平均值、记数等等。上述报表控件中常用公共属性有用于控制位置及高度宽度的Top、Left、Height、Width和控制可见性的Visible;其中RptTextBox还有DataField、DataMember、DataFormat及Font属性;其他属性不再多说。 然后介绍一下我的使用经验。一是对想控制的报表控件按类型有规律的命名;二是用RptShape的矩形框做表格线框,比用RptLine画框省事多了,只有斜线才使用RptLine;三是报表标题及报表中的表头文字、日期及页码用RptLabel,其中Caption属性支持转义字符,%D为长格式日期,%d为短格式日期,%P为总页数,%p为当前页码;四是对固定报表在设计窗口直接将报表控件摆放到位,对于活报表,应首先考虑报表最大的情形,将足够的控件分别放置在不同区域,位置大小可以不必深究,然后在报表输出前用VBA代码对所有控件的属性进行调整,包括位置、高度、宽度、字体、对齐方式、显示格式、可见性等等,相应的对Section也应根据情况调整其高度和可见性。 最后用一个实例模板来说明其使用方法。 连接数据库With 数据环境.rsCommand名If .State = adStateOpen Then .Close.Source = SQL语句 .Open 打开想输出的数据库数据项以便输出End WithWith 报表名 .DataSource=数据环境 .DataMember=Command名 这两行也可固定设好而不必每次设置 设置页表头部分(RpttLabel为报表控件名) .Sections(2).Controls(RptLabelPage).Caption = 共%P页第%p页 .Sections(2).Controls(RptLabelDate).Caption = 打印日期:%D .Sections(3).Controls(RptLabel1).Left= 设置细节部分(RptShapeX、RptTextBoxX为报表控件名) .Sections(3).Controls(RptShape1).Left= .Sections(3).Controls(RptShape1).Top= .Sections(3).Controls(RptShape1).Height= .Sections(3).Controls(RptShape1).Width= .Sections(3).Controls(RptTextBox1).DataMember=Command名 .Sections(3).Controls(RptTextBox1).DataField=字段1 .Sections(3).Controls(RptTextBox1).Font.Name= .Sections(3).Controls(RptShapeN).Visible=False .Sections(3).Controls(RptTextBoxN).Visible=False . Sections(3).Height=计算出的或固定的细节高度动态调整报表标题(RptLabelTitle为报表标签控件名) .Sections(2). Controls(RptLabelTitle).Left= .Sections(2). Controls(RptLabelTitle).Alignment= 调整完毕后 .Show 或 .PrintReportEnd With 这样做的优点是报表设计时简单,调整方便、随意,只需更改一点代码,而不必为了一点点的修改而费神的在设计窗口调整半天。 以上代码要写在 DataReport 窗体里的Load事件。rsCommand名 是DataReport数据环境里设置的命令名。就是Command。2.我们在数据库的编程中经常会用到报表的打印,其中datareport就是一个一个打印报表的控件,如果想要比较好的打印效果那么就要用到水晶报表了。那么首先datareport是怎么打印出报表来的呢?(一)首先在引用中选择mocrosoft data report designet v6.0然后在设计器里添加控件。(二)在窗体里调用设计器进行打印。例子代码如下:dim ff as instorereportSet ff.DataSource = rs If Cmbstore.Text Or Cmbstore.Text Null Then ff.Sections(section2).Controls.Item(lostore).Caption = Cmbstore.Text Else ff.Sections(section2).Controls.Item(lostore).Caption = End If If Cmbistore.Text Or Cmbistore.Text Null Then ff.Sections(section2).Controls.Item(listore).Caption = Cmbistore.Text Else ff.Sections(section2).Controls.Item(listore).Caption = End If If Tdjh.Text Or Tdjh.Text Null Then ff.Sections(section2).Controls.Item(ldjh).Caption = Tdjh.Text Else ff.Sections(section2).Controls.Item(ldjh).Caption = End If temp.Open select * from CompanyInfo , cn, 3, 2 If temp.EOF = False Then ff.Sections(section2).Controls.Item(LCompany).Caption = Trim(temp.Fields(0) 公司名称 End If ff.Sections(section2).Controls.Item(LTitle).Caption = 单据 ff.Caption = 打印 temp.Close 首先介绍下DataReport对象几个常用属性是DataSource用于设置个数据源通过该数据源数据使用者被绑定到个数据库; 2是DataMember从DataSource提供几个数据成员中设置个特定数据成员;3是LeftMargin、RightMargin、TopMargin、BottomMargin等用于指定报表左右上下页边距; 4是Sections即DataaReport报表标头、页标头、细节、页脚注、报表脚注5个区域如果加上分组(可以有多层分组)则增加对区域即分组标头、分组脚注其中DataSource般是个数据环境或是ADODB.Connection类型变量而DataMember则对应数据环境中Command或是ADODB.RecordSet类型变量推荐使用数据环境及Command页边界大家肯定都很清楚下面我主要介绍以下Sections这也是DataReport精髓所在 Sections是个集合您可以为每个Section指定名称也可以用其缺省索引从上到下依次为1、2每个Section均有Height和Visible属性您可以在定条件下使个Section不可见在Section中可以放置各种报表Control控件其中RptLabel、RptImage、RptShape和RptLine可以放在任意Section中用于输出各种文字、图形及表格线;RptTextBox只能放在细节中般用于绑定输出DataMemeber提供数据字段;RptFunction只能被放置在分组注脚中,用于输出使用各种内置计算出合计、最大值、最小值、平均值、记数等等上述报表Control控件中常用公共属性有用于控制位置及高度宽度Top、Left、Height、Width和控制可见性Visible;其中RptTextBox还有DataField、DataMember、DataFormat及Font属性;其他属性不再多说 然后介绍下我使用经验是对想控制报表Control控件按类型有规律命名; 2是用RptShape矩形框做表格线框比用RptLine画框省事多了只有斜线才使用RptLine; 3是报表标题及报表中表头文字、日期及页码用RptLabel其中Caption属性支持转义%D为长格式日期%d为短格式日期%P为总页数%p为当前页码(注意:D和d,P和p大小写的区别); 4是对固定报表在设计窗口直接将报表Control控件摆放到位对于活报表应首先考虑报表最大情形将足够Control控件分别放置在区别区域位置大小可以不必深究然后在报表输出前用VBA代码对所有Control控件属性进行调整包括位置、高度、宽度、字体、对齐方式、显示格式、可见性等等相应对Section也应根据情况调整其高度和可见性最后用个例子模板来介绍说明其使用思路方法 连接数据库 With 数据环境.rsCommand名 If .State = adStateOpen Then .Close .Source = SQL语句 .Open 打开想输出数据库数据项以便输出 End With With 报表名 .DataSource=数据环境 .DataMember=Command名 这两行也可固定设好而不必每次设置 设置页表头部分(RpttLabel为报表Control控件名) .Sections(2).Controls(RptLabelPage).Caption = 共%P页第%p页 .Sections(2).Controls(RptLabelDate).Caption = 打印日期:%D .Sections(3).Controls(RptLabel1).Left= 设置细节部分(RptShapeX、RptTextBoxX为报表Control控件名) .Sections(3).Controls(RptShape1).Left= .Sections(3).Controls(RptShape1).Top= .Sections(3).Controls(RptShape1).Height= .Sections(3).Controls(RptShape1).Width= .Sections(3).Controls(RptTextBox1).DataMember=Command名 .Sections(3).Controls(RptTextBox1).DataField=字段1 .Sections(3).Controls(RptTextBox1).Font.Name= .Sections(3).Controls(RptShapeN).Visible=False .Sections(3).Controls(RptTextBoxN).Visible=False . Sections(3).Height=计算出或固定细节高度 动态调整报表标题(RptLabelTitle为报表标签Control控件名) .Sections(2). Controls(RptLabelTitle).Left= .Sections(2). Controls(RptLabelTitle).Alignment= 调整完毕后 .Show 或 .PrReport End With 这样做优点是报表设计时简单调整方便、随意只需更改点代码而不必为了点点修改而费神在设计窗口调整半天vb6.0 中,用datareport 报表控件制作报表,纸张定义 A4 A3 在vb6.0中,用datareport 报表控件制作报表,当使用报表预览功能时,datareport报表控件将使用windows系统的当前默认打印机的打印设置,若该打印设置纸张尺寸(如A4纸)小于datareport报表控件所需纸张尺寸(如A3纸),则datareport 报表预览时将会出现打印纸张尺寸小于报表宽度错误,请教如何用代码设置windows系统的当前默认打印机的打印机纸张尺寸,而不需用公共对话框的打印设置方法? Option Explicit Public Enum PrinterOrientationConstants OrientPortrait = 2 OrientLandscape = 1 End Enum Printer.PaperSize = vbPRPSA3 Private Type DEVMODE dmDeviceName As String * 32 dmSpecVersion As Integer dmDriverVersion As Integer dmSize As Integer dmDriverExtra As Integer dmFields As Long dmOrientation As Integer dmPaperSize As Integer dmPaperLength As Integer dmPaperWidth As Integer dmScale As Integer dmCopies As Integer dmDefaultSource As Integer dmPrintQuality As Integer dmColor As Integer dmDuplex As Integer dmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * 32 dmUnusedPadding As Integer dmBitsPerPel As Integer dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type Private Type PRINTER_DEFAULTS pDataType As String pDevMode As Long DesiredAccess As Long End Type Private Type PRINTER_INFO_2 pServerName As Long pPrinterName As Long pShareName As Long pPortName As Long pDriverName As Long pComment As Long pLocation As Long pDevMode As Long pSepFile As Long pPrintProcessor As Long pDataType As Long pParameters As Long pSecurityDescriptor As Long Attributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long cJobs As Long AveragePPM As Long End Type Private Const DC_PAPERNAMES = 16 Private Const DC_PAPERS = 2 Private Const DC_PAPERSIZE = 3 Private Const DM_IN_BUFFER = 8 Private Const DM_OUT_BUFFER = 2 Private Const DM_ORIENTATION = &H1 Private Const DM_PAPERSIZE = &H2& Private Const DMPAPER_A3 = 8 A3 297 x 420 mm Private Const DMPAPER_A4 = 9 A4 210 x 297 mm Private Const PRINTER_ACCESS_ADMINISTER = &H4 Private Const PRINTER_ACCESS_USE = &H8 Private Const STANDARD_RIGHTS_REQUIRED = &HF0000 Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE) Private Declare Sub CopyMemory Lib kernel32 Alias RtlMoveMemory _ (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Private Declare Function OpenPrinter Lib winspool.drv Alias _ OpenPrinterA (ByVal pPrinterName As String, phPrinter As _ Long, pDefault As Any) As Long Private Declare Function ClosePrinter Lib winspool.drv _ (ByVal hPrinter As Long) As Long Private Declare Function DocumentProperties Lib winspool.drv _ Alias DocumentPropertiesA (ByVal hWnd As Long, ByVal hPrinter As Long, _ ByVal pDeviceName As String, pDevModeOutput As Any, pDevModeInput As Any, _ ByVal fMode As Long) As Long Private Declare Function GetPrinter Lib winspool.drv _ Alias GetPrinterA (ByVal hPrinter As Long, ByVal Level As Long, _ pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long Private Declare Function SetPrinter Lib winspool.drv _ Alias SetPrinterA (ByVal hPrinter As Long, ByVal Level As Long, _ pPrinter As Any, ByVal Command As Long) As Long Private Declare Function DeviceCapabilities Lib winspool.drv _ Alias DeviceCapabilitiesA (ByVal lpDeviceName As String, ByVal lpPort As String, _ ByVal iIndex As Long, ByVal lpOutput As String, lpDevMode As DEVMODE) As Long Function SetDefaultPrinterOrientation(ByVal eOrientation As _ PrinterOrientationConstants, ByVal strPaperSize As String) As Boolean Dim bDevMode() As Byte Dim bPrinterInfo2() As Byte Dim hPrinter As Long Dim lResult As Long Dim nSize As Long Dim sPrnName As String Dim dm As DEVMODE Dim pd As PRINTER_DEFAULTS Dim pi2 As PRINTER_INFO_2 Get device name of default printer sPrnName = Printer.DeviceName PRINTER_ALL_ACCESS required under NT, because were going to call SetPrinter pd.DesiredAccess = PRINTER_ALL_ACCESS Get a handle to the printer. If OpenPrinter(sPrnName, hPrinter, pd) Then Get number of bytes requires for PRINTER_INFO_2 structure Call GetPrinter(hPrinter, 2&, 0&, 0&, nSize) Create a buffer of the required size ReDim bPrinterInfo2(1 To nSize) As Byte Fill buffer with structure lResult = GetPrinter(hPrinter, 2, bPrinterInfo2(1), _ nSize, nSize) Copy fixed portion of structure into VB Type variable Call CopyMemory(pi2, bPrinterInfo2(1), Len(pi2) Get number of bytes requires for DEVMODE structure nSize = DocumentProperties(0&, hPrinter, sPrnName, _ 0&, 0&, 0) Create a buffer of the required size ReDim bDevMode(1 To nSize) If PRINTER_INFO_2 points to a DEVMODE structure, copy it into our buffer If pi2.pDevMode Then Call CopyMemory(bDevMode(1), ByVal pi2.pDevMode, Len(dm) Else Otherwise, call DocumentProperties to get a DEVMODE structure Call DocumentProperties(0&, hPrinter, sPrnName, _ bDevMode(1), 0&, DM_OUT_BUFFER) End If Copy fixed portion of structure into VB Type variable Call CopyMemory(dm, bDevMode(1), Len(dm) With dm Set new orientation Select Case strPaperSize Case A3 .dmPaperSize = DMPAPER_A3 Case A4 .dmPaperSize = DMPAPER_A4 End Select .dmOrientation = eOrientation .dmFields = DM_ORIENTATION + DM_PAPERSIZE End With Copy our Type back into buffer Call CopyMemory(bDevMode(1), dm, Len(dm) Set new orientation Call DocumentProperties(0&, hPrinter, sPrnName, _ bDevMode(1), bDevMode(1), DM_IN_BUFFER Or _ DM_OUT_BUFFER) Point PRINTER_INFO_2 at our modified DEVMODE pi2.pDevMode = VarPtr(bDevMode(1) Set new orientation system-wide lResult = SetPrinter(hPrinter, 2, pi2, 0&) Clean up and exit Call ClosePrinter(hPrinter) SetDefaultPrinterOrientation = True Else SetDefaultPrinterOrientation = False End If End Function-njx:窗体调用的代码:打印纸选用:1:纵向 2.横向SetDefaultPrinterOrientation 2, A3-1.报表的概念 利用报表可以把数据表中的数据按一定的格式输出到屏幕上或打印到纸上。 2.制作报表的方法 在VB6.0中可以利用报表设计器来制作报表,从“工程”中选择“添加data report”,将报表设计器加入到当前工程中,报表由5部分组成: 报表标头每份报表只有一个,可以用标签建立报表名。 页标头每页有一个,即每页的表头,如字段名。 细节需要输出的具体数据,一行一条记录。 页脚注每页有一个,如页码。 报表脚注每份报表只有一个,可以用标签建立对本报表的注释、说明。 使用报表设计器处理的数据需要利用数据环境设计器创建与数据库的连接, 从“工程”菜单中选择 “添加Data Enviroment”,在连接中选择指定的数据库文件,完成与数据库的连接,然后产生Command对象连接数据库内的表。 制作报表的步骤: (1) 新建工程,在窗体上放置两个命令按钮; (2) 从“工程”菜单中“添加Data Enviroment”,右击Connection1,在属性中选择“MicrosoftJet 4 OLE DB Provider”,在“连接”中指定数据库; (3) 再次右击Connection1,选则“添加命令”,创建Command1对象,右击Command1,在属性中设置该对象连接的数据源为需要打印的数据表; (4) 在从“工程”菜单中“添加Data Report”,在属性窗口中设置DataSource为数据环境DataEnviroment1对象,DataMember为Command1对象,即指定数据报表设计器DataReport1的数据来源; (5) 将数据环境设计器中Command1对象内的字段拖到数据报表设计器的细节区; (6) 利用标签控件在报表标头区插入报表名,在页标头区设置报表每一页顶部的标题; (7) 利用线条控件在报表内加入直线,利用图形控件和形状控件加入图案或图形; (8) 利用DataReport1对象的Show方法显示报表,在窗体Click事件加代码:DataReport1.Show; (9) 利用预览窗口按打印按钮可以打印报表; (10)利用预览窗口工具栏上的导出按钮可以将报表内容输出成文本文件或Html文件;也可以利用DataReport1对象的ExportReport方法将报表内容输出成文本文件或Html文件。 制作报表的简单方法是从“外接程序”中选择报表向导来设计报表。水晶报表(Crystal Report)是业内最专业、功能最强的报表系统,它除了强大的报表功能外,最大的优势是实现了与绝大多数流行开发工具的集成和接口。在VS.Net平台做过报表开发的程序员,一定都对水晶报表强大、高效、集成等特性留下了深刻印象。除了开发新程序外,在工作中我们常需要接触到很多较早的软件系统报表功能升级的需求,如果能结合水晶报表这一强大的工具,往往能事半功倍。VB是以前流行的数据库开发平台,用其开发的C/S系统在社会上有非常大的保有量,但VB超弱的报表功能往往让程序员面对客户的升级要求一筹莫展。本文并不做水晶报表的使用和编程教学,实际上水晶报表本身的使用方法和VS.Net平台并没有太大的差别,我主要是和大家探讨一下VB和水晶报表的一种较方便的接口方式。我的开发测试平台是Windows2003 Standard简体中文版、VB6.0+sp5英文版、Crystal 9.简体中文开发版。关于在水晶报表中制作报表模板的方法,并非本文的探讨范围,读者可以参考Crystal Report的帮助文件和官方网站的技术资料。简单得说,首先要通过水晶报表的数据库引擎手动连接相应的表结构,制作报表模板,并保存为rpt文件,该项操作和利用VB自带的报表工具制作报表大同小异。简单地说,用VB调用水晶报表进行报表开发的简单接口方法就是,在水晶报表中用仅字段定义来获得字段分布文件,用虚拟的文件创建表字段,用CRAXDRT对象来强制改变数据源(ADO.recordset),其效果相当于在VB中调用了rpt文件。下面分步骤介绍编程方法。第一步:在VB工程中Project菜单加入Add Crystal Report 9,报表名使用默认即可。这时Form2(CrystalRerport自动添加的Form,假设名为Form2)被自动分配了如下代码:Option Explicitdim Report as New Cystal1Private Sub Form_Load() Screen.MousePointer = vbHourglass 调用水晶报表时置鼠标为沙漏状 CRViewer91.ReportSource = Report 该语句的赋值将在后面被修改 CRViewer91.ViewReport Screen.MousePointer = vbDefault 调用水晶报表完成后置鼠标为默认形状End SubPrivate Sub Form_Resize() CRViewer91.Top = 0 CRViewer91.Left = 0 CRViewer91.Height = ScaleHeight CRViewer91.Width = ScaleWidthEnd Sub第二步:点击Crystal Report设计器的数据库字段,选定数据库专家,然后点创建新连接,再点仅字段定义,创建数据库定义文件,字段名和宽度和原数据库表保持一致。最后,在数据库字段中获得了相应字段,将其置于报表上,按水晶报表的要求配置。第三步:该步骤非常关键,添加一个Modual到工程文件中,定义全局的ADODB变量,实现数据库和水晶报表的动态连接。代码如下:Public conn As New ADODB.ConnectionPublic rs As New ADODB.Recordset第四步:关于VB程序的ADO数据库连接注意事项,请看下面的打印按钮例程。Private Sub Command1_Click() Dim connstr As String If conn.State = adStateOpen Then conn.Close connstr = Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & App.Path & prtest.mdb;Persist Security Info=False prtest.mdb是程序当前目录的测试Access数据库 conn.ConnectionString = connstr conn.Open conn.CursorLocation = adUseClient If rs.State = adStateOpen Then rs.Close rs.Open test, conn, adOpenKeyset, adLockReadOnly Report.Database.SetDataSource rs, 3, 1 Form2.Show 1 数据库连接完成后,调用Form2水晶报表工程End Sub需要提请大家注意的是,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论