.Net報表開發(fā)控件XtraReport介紹
?一、概述
在XtraReport中,每一個報表都是XtraReport或者其子類。
XtraReport中的報表類可以與數(shù)據(jù)綁定也可以不綁定。
在創(chuàng)建一個報表時,可以從已有的報表中加載樣式和布局,樣式中包含了報表控件外觀的屬性值,而布局包含了報表的結(jié)構(gòu)信息。另外,還可以從其他報表系統(tǒng)中導(dǎo)入報表,比如:Access,水晶報表等等。
報表類(XtraReport的子類)創(chuàng)建后,就可以生成其實例。需要注意的是,XtraReport對象可以在Windows Forms中使用也可以在Asp.net中使用。在Windows應(yīng)用中使用報表,通常需要維護報表的,這個對象提供了報表的輸出功能。
創(chuàng)建報表有兩種方式,一種是簡單地添加一個"模板"報表,一種是通過報表向?qū)韯?chuàng)建報表。
在報表添加到項目后,報表設(shè)計器提供了大量的設(shè)計時元素來加快簡化報表的創(chuàng)建。XtraReport工具箱包含了所有的控件。
Report Navigator可以瀏覽整個報表,F(xiàn)eild List可以拖放數(shù)據(jù)字段來創(chuàng)建與數(shù)據(jù)綁定的報表控件。
XtraReport的所有報表都是由和組成的。
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)建報表的結(jié)構(gòu),首先在XtraReportBase.Bands屬性中添加Bands,然后在相應(yīng)的Bands的XRControl.Controls屬性中添加控件。報表帶和控件的添加方法一般是這樣的。
// 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 });
可以給報表傳遞參數(shù):
XtraReport1 report = new XtraReport1(); report.Parameters["yourParameter1"].Value = firstValue; report.Parameters["yourParameter2"].Value = secondValue;
報表內(nèi)使用參數(shù):
report.FilterString = "[CategoryID] = [Parameters.yourParameter1]";
最后創(chuàng)建好的報表可以輸出給用戶看了
// 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);
二、實例展示
1、報表設(shè)計
在報表屬性中,設(shè)置默認font為微軟雅黑,設(shè)置Language為默認(注意:不能設(shè)置為中文,否則導(dǎo)出PDF中文亂碼)。
2、報表后臺代碼
綁定到List類型的綁定方法,設(shè)置報表上的對象的XRBinging中DataSource參數(shù)為null。
從報表的DataMember 需要設(shè)置,否則從報表只顯示一條記錄。
同時“計算”字段的表達式的使用。
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)用報表
List list = new List<MyEntity> { entity }; MyReport myReport = new MyReport(); //報表實例 myReport.DataSource = list;//綁定報表的數(shù)據(jù)源 myReport.ShowPreview();
到此這篇關(guān)于.Net報表開發(fā)控件XtraReport的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net FindControl方法誤區(qū)和解析
在ASP.NET中Control都有一個FindControl方法,其作用是根據(jù)ID(注意既不是UniqueID也不是ClientID)在Control所在的命名容器中尋找相應(yīng)控件,但實際使用中存在很多誤區(qū)和陷阱,下面談?wù)剛€人對此的理解2012-01-01.Net行為型設(shè)計模式之備忘錄模式(Memento)
這篇文章介紹了.Net行為型設(shè)計模式之備忘錄模式(Memento),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05