căn bản crystal reports trong lập trình c#

Download Căn bản crystal reports trong lập trình C#

Post on 25-Aug-2014

148 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Crystal report C#

TRANSCRIPT

Ngun: congdongcviet.com Cn bn crystal reports trong lp trnh C#Hm nay tui s dnh vit my bi tut c bn nht v crystal reports! u tin, ta to 1 database nh lm vic vi n, cc bn to trong sqlserver 1 database c tn l example, c 3 table l OrderMaster , OrderDetails ,ProductPHP Code:

USE [example] GO /****** Object: Table [dbo].[OrderMaster] Script Date: 09/04/2009 15:11:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[OrderMaster]( [OrderMaster_id] [int] NOT NULL, [OrderMaster_date] [datetime] NULL, [OrderMaster_customername] [varchar](50) NULL, [OrderMaster_createduser] [varchar](50) NULL, CONSTRAINT [PK_OrderMaster] PRIMARY KEY CLUSTERED ( [OrderMaster_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[OrderDetails] Script Date: 09/04/2009 15:11:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[OrderDetails]( [OrderDetails_id] [int] NOT NULL, [OrderDetails_masterid] [int] NULL, [OrderDetails_productid] [int] NULL, [OrderDetails_qty] [int] NULL, CONSTRAINT [PK_OrderDetails] PRIMARY KEY CLUSTERED ( [OrderDetails_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS ) ON [PRIMARY] GO /****** Object: Table [dbo].[Product] Script Date: 09/04/2009 15:11:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Product]( [Product_id] [int] NOT NULL, [Product_name] [varchar](50) NULL, [Product_price] [numeric](18, 0) NULL, CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ( [Product_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO To 1 project winform application trong VS. Vo menu PROJECT-->Add New Item. Chn item l Crytal Report Hin ra 1 bng chn mu bo co, y ti s mc nh

= ON, ALLOW_PAGE_LOCKS

= ON) ON [PRIMARY]

= ON, ALLOW_PAGE_LOCKS

= ON) ON [PRIMARY]

= ON, ALLOW_PAGE_LOCKS

= ON) ON [PRIMARY]

Tip n l chn kt ni ti database ( y l example), ti s dng OLEDB Connection kt ni. Chn OLEDB connection hp thoi Create New Connection

in cc thng tin kt ni vo :

hp thoi k tip s hin cc thng s kt ni, c vic Finish y ti mun hin th table Product, nn chn table Product

Chn cc trng ca table product

Chn next v chn cc kiu hnh thc ca report, cui cng l Finish

xong bc to report, trn Form1 chng ta to lc ny,ko v 1 ci Button v 1 ci CrystalReportViewer control,khi click v button th s hin ra bo co :PHP Code:

ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(@"C:\Documents and Settings\Qu@n Bom\My Documents\Visual Studio 2008\Projects\Example\Example\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); Qun, nh l phi using CrystalDecisions.CrystalReports.Engine;

__________________ Cnh Chym mi

dieu cay555 Xem H s cng khai Gi mt tin nhn ti dieucay555 Tm ton b bi vit bi dieucay555 #2 04-09-2009, 04:08 PM

dieucay555

Anonymous Member

Ngy gia nhp: 11 2007 Ni : Bit lm g? Bi vit: 950

To reports t nhiu bngBi trn ta to report ch vi 1 bng l Product, by gi s to report vi nhiu bng xem sao nh Mi vic cng ging nh trn, n bc chn cc table th ta chn c 3 table cho n mu , sau y l chn cc fields mun report :

Mi chuyn li tip tc nh th P/s : bi hng dn vn cha ht nn mng cc bn cha hi y, tm thi tui s lock ti li vit tip __________________ Cnh Chym mi c chnh sa ln cui bi dieucay555 : 04-09-2009 lc 04:12 PM. dieu cay555 Xem H s cng khai Gi mt tin nhn ti dieucay555 Tm ton b bi vit bi dieucay555 #3 04-09-2009, 11:27 PM

dieucay555

Anonymous Member

Ngy gia nhp: 11 2007 Ni : Bit lm g? Bi vit: 950

S dng parameter trong crytal report bi ny ti s hng dn cch s dng String parameter. V d,bn mun a vo 1 tham s l customername,v s hin ra cc n t hng ca khch hng report. y, tui s s dng tip ci report to bi trn. Chn Fields Explorer (menu Crytal Reports ->Fields Explorer),chut phi vo Parameter Fields,ri new,in cc tham s vo :

Ti chn kiu l string, cc kiu khc lm cng tng t Sau khi to xong parameter, ta s to biu thc cho cc fields,vo ca s design ca crytal reports ri lm nh hnh :

Hin ra 1 editor bn thit k fomular, mun s dng field no th chn field ,cc bn c lm theo th t t onh s trong hnh nh

Thit k form nh trong hnh :

Bi gi ta vit code cho s kin ca nt show Visual C# Code: La chn code | n/Hin code private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(@"C:\Documents and Settings\Qu@n Bom\My Documents\Visual Studio 2008\Projects\Example\Example\CrystalReport1.rpt"); ParameterFieldDefinitions crParameterFieldDefinitions; ParameterFieldDefinition crParameterFieldDefinition; ParameterValues crParameterValues = new ParameterValues(); ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue(); crParameterDiscreteValue.Value = textBox1.Text; crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields; crParameterFieldDefinition = crParameterFieldDefinitions["Customername"]; crParameterValues = crParameterFieldDefinition.CurrentValues; crParameterValues.Clear(); crParameterValues.Add(crParameterDiscreteValue); crParameterFieldDefinition.ApplyCurrentValues(crParameterValues); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } Cc file nh km Example.rar (62.3 KB, 2191 ln xem) __________________ Cnh Chym midieu cay555 Xem H s cng khai Gi mt tin nhn ti dieucay555 Tm ton b bi vit bi dieucay555 #4 05-09-2009, 08:48 AM

dieucay555Anonymous Member

Ngy gia nhp: 11 2007 Ni : Bit lm g? Bi vit: 950

To reports t DataSetMy bi trc tui ch to report t database, bi ny tui s to 1 report t DataSet coi sao nh . To 1 project winform mi, add thm vo 1 DataSet,gi nguyn cc thng tin mc nh ca n,trn DataSet ta to ra 1 datatable c 2 column l ID v Item :

By gi ta s to 1 report t DataSet mi to ra. Vo menu Project->Add new item, chn crytal report nh my ci trn kia, cc thng s mc nh lun cho tin. Nhng y ta ko to ra connect ti database no m chn lun dataset lc ny :

bc k tip, chn lun c 2 column ca DataTable trn DataSet lc ny nh

By gi to thm vo form 1 ci button v 1 crystalReportViewer control nh my ci trc. Tui s to d liu n gin cho dataset v hin th report : Visual C# Code: La chn code | n/Hin code private void button1_Click(object sender, EventArgs e) { DataSet1 ds = new DataSet1();

DataTable t = ds.Tables.Add("Items"); t.Columns.Add("id", Type.GetType("System.Int32")); t.Columns.Add("Item", Type.GetType("System.String")); DataRow r; int i = 0; for (i = 0; i ADO.NET ri chn n file XML ny nh, cng vic cn li ko khc g bi u tin :

La chn code | n/Hin code

__________________ Cnh Chym midieu cay555 Xem H s cng khai Gi mt tin nhn ti dieucay555 Tm ton b bi vit bi dieucay555 #8 05-09-2009, 12:24 PM

dieucay555Anonymous Member

Ngy gia nhp: 11 2007 Ni : Bit lm g? Bi vit: 950

Xut kt qu report ra Excel,PDFc my bi trn cc bn chc nm c s s v crytal report ri, gi ni thm v vic trch xut ra cc nh dng ca Excel hoc PDF Cc bn c th s dng ExportOptions ty chnh cc nh dng trch xut Quay li bi u tin, ta thm 2 ci button na,sau khi to v hin th thnh cng report, ta c th export ra pdf hoc excel y l export ra 1 file excel (file test.xls s dc to C) Visual C# Code: try { ExportOptions CrExportOptions; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions(); CrDiskFileDestinationOptions.DiskFileName = "c:\\test.xls"; CrExportOptions = cryRpt.ExportOptions;

La chn code | n/Hin code

} catch (Exception ex) { MessageBox.Show(ex.ToString()); } Export ra pdf : Visual C# Code: try {

CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; CrExportOptions.ExportFormatType = ExportFormatType.Excel; CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; cryRpt.Export();

La chn code | n/Hin code ExportOptions CrExportOptions; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions(); CrDiskFileDestinationOptions.DiskFileName = "C:\\test.pdf"; CrExportOptions = cryRpt.ExportOptions; { CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; } cryRpt.Export();

} catch (Exception ex) { MessageBox.Show(ex.ToString()); } , nhPHP Code:

using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;

[C#]Hng dn to Crystal Reports bng DatasetT l bu chnGii thiu Hin ti c rt t bi ting vit hng dn v phn ny. creating Crystal Reports using C#.NET . To mt Crystal Reports trong C#.Net vi CSDL access SQL sercer lm tng t. To dataset u tin bn to 1 d n Sau Add New Item trong Project menu, bn add a Dataset Ging hnh bn di. Ngun bi vit: http://sinhvienit.net/@forum/showthread.php?t=16748

Gii thiu Hin ti c rt t bi ting vit hng dn v phn ny. creating Crystal Reports using C#.NET . To mt Crystal Reports trong C#.Net vi CSDL access SQL sercer lm tng t. To dataset

u tin bn to 1 d n Sau Add New Item trong Project menu, bn add a Dataset Ging hnh bn di.

Code:String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_ Data Source=..\\..\\myDB.mdb;User ID=Admin;Password="; OleDbConnection conn = new OleDbConnection(connString); conn.Open(); string query = "SELECT studentID, firs