.Net報(bào)表開發(fā)控件XtraReport介紹
?一、概述
在XtraReport中,每一個(gè)報(bào)表都是XtraReport或者其子類。
XtraReport中的報(bào)表類可以與數(shù)據(jù)綁定也可以不綁定。
在創(chuàng)建一個(gè)報(bào)表時(shí),可以從已有的報(bào)表中加載樣式和布局,樣式中包含了報(bào)表控件外觀的屬性值,而布局包含了報(bào)表的結(jié)構(gòu)信息。另外,還可以從其他報(bào)表系統(tǒng)中導(dǎo)入報(bào)表,比如:Access,水晶報(bào)表等等。
報(bào)表類(XtraReport的子類)創(chuàng)建后,就可以生成其實(shí)例。需要注意的是,XtraReport對(duì)象可以在Windows Forms中使用也可以在Asp.net中使用。在Windows應(yīng)用中使用報(bào)表,通常需要維護(hù)報(bào)表的,這個(gè)對(duì)象提供了報(bào)表的輸出功能。
創(chuàng)建報(bào)表有兩種方式,一種是簡(jiǎn)單地添加一個(gè)"模板"報(bào)表,一種是通過報(bào)表向?qū)韯?chuàng)建報(bào)表。
在報(bào)表添加到項(xiàng)目后,報(bào)表設(shè)計(jì)器提供了大量的設(shè)計(jì)時(shí)元素來加快簡(jiǎn)化報(bào)表的創(chuàng)建。XtraReport工具箱包含了所有的控件。
Report Navigator可以瀏覽整個(gè)報(bào)表,F(xiàn)eild List可以拖放數(shù)據(jù)字段來創(chuàng)建與數(shù)據(jù)綁定的報(bào)表控件。
XtraReport的所有報(bào)表都是由和組成的。
public class XtraReport1 : DevExpress.XtraReports.UI.XtraReport { private DevExpress.XtraReports.UI.DetailBand Detail; private DevExpress.XtraReports.UI.PageHeaderBand PageHeader; private DevExpress.XtraReports.UI.PageFooterBand PageFooter; private DevExpress.XtraReports.UI.XRLabel xrLabel1; private DevExpress.XtraReports.UI.XRLabel xrLabel2; private System.ComponentModel.Container components = null; public XtraReport1() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } }
然后開始創(chuàng)建報(bào)表的結(jié)構(gòu),首先在XtraReportBase.Bands屬性中添加Bands,然后在相應(yīng)的Bands的XRControl.Controls屬性中添加控件。報(bào)表帶和控件的添加方法一般是這樣的。
// Add Detail, PageHeader and PageFooter bands to the report's collection of bands. this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { this.Detail, this.PageHeader, this.PageFooter }); // Add two XRLabel controls to the Detail band. this.Detail.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] { this.xrLabel1, this.xrLabel2 });
可以給報(bào)表傳遞參數(shù):
XtraReport1 report = new XtraReport1(); report.Parameters["yourParameter1"].Value = firstValue; report.Parameters["yourParameter2"].Value = secondValue;
報(bào)表內(nèi)使用參數(shù):
report.FilterString = "[CategoryID] = [Parameters.yourParameter1]";
最后創(chuàng)建好的報(bào)表可以輸出給用戶看了
// Create a report. XtraReport1 report = new XtraReport1(); // Create the report's document so it can then be previewed, printed or exported. // NOTE: Usually you don't need to call this method as it's automatically called by all of the following methods. // See the corresponding member topic to find out when it needs to be called. report.CreateDocument(); // Show the form with the report's print preview. report.ShowPreview(); // Print the report in a dialog and "silent" mode. report.PrintDialog(); report.Print(); // Open the report in the End-User designer report.RunDesigner(); // Export the report. report.CreateHtmlDocument("report.html"); report.CreatePdfDocument("report.pdf"); report.CreateImage("report.jpg", System.Drawing.Imaging.ImageFormat.Gif);
二、實(shí)例展示
1、報(bào)表設(shè)計(jì)
在報(bào)表屬性中,設(shè)置默認(rèn)font為微軟雅黑,設(shè)置Language為默認(rèn)(注意:不能設(shè)置為中文,否則導(dǎo)出PDF中文亂碼)。
2、報(bào)表后臺(tái)代碼
綁定到List類型的綁定方法,設(shè)置報(bào)表上的對(duì)象的XRBinging中DataSource參數(shù)為null。
從報(bào)表的DataMember 需要設(shè)置,否則從報(bào)表只顯示一條記錄。
同時(shí)“計(jì)算”字段的表達(dá)式的使用。
public partial class MyReport : DevExpress.XtraReports.UI.XtraReport { public MyReport() { InitializeComponent(); this.xrLabel6.DataBindings.Add("Text", null, "FileNo"); this.xrLabel7.DataBindings.Add("Text", null, "ApplyTime", "{0:yyyy-MM-dd}"); CalculatedField calculatedField1 = new CalculatedField { Expression = "Iif([InspectItms.InspectResult]==1,'合格','不合格' )", Name = "calculatedField1" }; CalculatedField calculatedField2 = new CalculatedField { Expression = "[ReviewResult]=='1'", Name = "calculatedField2" }; this.CalculatedFields.AddRange(new DevExpress.XtraReports.UI.CalculatedField[] { calculatedField1, calculatedField2}); DetailReport.DataMember = "InspectItms"; this.xrTableCell5.DataBindings.Add("Text", null, "InspectItms.InspectItem"); this.xrTableCell6.DataBindings.Add("Text", null, "calculatedField1"); this.xrCheckBox1.DataBindings.Add("Checked", null, "calculatedField2"); } }
3、調(diào)用報(bào)表
List list = new List<MyEntity> { entity }; MyReport myReport = new MyReport(); //報(bào)表實(shí)例 myReport.DataSource = list;//綁定報(bào)表的數(shù)據(jù)源 myReport.ShowPreview();
到此這篇關(guān)于.Net報(bào)表開發(fā)控件XtraReport的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net FindControl方法誤區(qū)和解析
在ASP.NET中Control都有一個(gè)FindControl方法,其作用是根據(jù)ID(注意既不是UniqueID也不是ClientID)在Control所在的命名容器中尋找相應(yīng)控件,但實(shí)際使用中存在很多誤區(qū)和陷阱,下面談?wù)剛€(gè)人對(duì)此的理解2012-01-01.Net行為型設(shè)計(jì)模式之備忘錄模式(Memento)
這篇文章介紹了.Net行為型設(shè)計(jì)模式之備忘錄模式(Memento),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05asp.net平臺(tái)下C#實(shí)現(xiàn)Socket通信
這篇文章介紹了asp.net平臺(tái)下C#實(shí)現(xiàn)Socket通信的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01