.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)表,一種是通過(guò)報(bào)表向?qū)?lái)創(chuàng)建報(bào)表。

在報(bào)表添加到項(xiàng)目后,報(bào)表設(shè)計(jì)器提供了大量的設(shè)計(jì)時(shí)元素來(lái)加快簡(jiǎn)化報(bào)表的創(chuàng)建。XtraReport工具箱包含了所有的控件。

Report Navigator可以瀏覽整個(gè)報(bào)表,F(xiàn)eild List可以拖放數(shù)據(jù)字段來(lái)創(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?Core?MVC基礎(chǔ)系列之環(huán)境設(shè)置
這篇文章介紹了ASP.Net?Core?MVC環(huán)境設(shè)置的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
詳解ABP框架中領(lǐng)域?qū)拥念I(lǐng)域事件Domain events
ABP是基于ASP.NET框架之上的Web開發(fā)框架(GitHub:https://github.com/aspnetboilerplate),這篇我們來(lái)詳解ABP框架中領(lǐng)域?qū)拥念I(lǐng)域事件Domain events,需要的朋友可以參考下2016-06-06
通過(guò)VS中的數(shù)據(jù)源選擇對(duì)話框簡(jiǎn)單實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接配置
通過(guò)VS中的數(shù)據(jù)源選擇對(duì)話框簡(jiǎn)單實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接配置...2007-02-02
System.Diagnostics.Metrics .NET 6 全新指標(biāo)API講解
本文詳細(xì)講解了.NET 6全新指標(biāo)System.Diagnostics.Metrics,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
如何在WebForm中使用javascript防止連打(雙擊)
如何在WebForm中使用javascript防止連打(雙擊)...2007-01-01
ASP.NET?Core中的Configuration配置二
這篇文章介紹了ASP.NET?Core中的Configuration配置,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
.Net?6簡(jiǎn)介并和之前版本寫法做對(duì)比
這篇文章介紹了.Net?6簡(jiǎn)介并和之前版本寫法做對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
asp.net網(wǎng)絡(luò)數(shù)據(jù)庫(kù)開發(fā)實(shí)例精解 源文件
asp.net網(wǎng)絡(luò)數(shù)據(jù)庫(kù)開發(fā)實(shí)例精解 源文件...2006-09-09

