.Net报表开发控件XtraReport介绍

 更新时间:2022年06月02日 09:30:19   作者:springsnow  
这篇文章介绍了.Net报表开发控件XtraReport,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、概述

在XtraReport中,每一个报表都是XtraReport或者其子类。

XtraReport中的报表类可以与数据绑定也可以不绑定。

在创建一个报表时,可以从已有的报表中加载样式和布局,样式中包含了报表控件外观的属性值,而布局包含了报表的结构信息。另外,还可以从其他报表系统中导入报表,比如:Access,水晶报表等等。

报表类(XtraReport的子类)创建后,就可以生成其实例。需要注意的是,XtraReport对象可以在Windows Forms中使用也可以在Asp.net中使用。在Windows应用中使用报表,通常需要维护报表的,这个对象提供了报表的输出功能。

创建报表有两种方式,一种是简单地添加一个"模板"报表,一种是通过报表向导来创建报表。

在报表添加到项目后,报表设计器提供了大量的设计时元素来加快简化报表的创建。XtraReport工具箱包含了所有的控件。

Report Navigator可以浏览整个报表,Feild List可以拖放数据字段来创建与数据绑定的报表控件。

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);
    }
}

然后开始创建报表的结构,首先在XtraReportBase.Bands属性中添加Bands,然后在相应的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 });

可以给报表传递参数:

XtraReport1 report = new XtraReport1();  
report.Parameters["yourParameter1"].Value = firstValue;  
report.Parameters["yourParameter2"].Value = secondValue;

报表内使用参数:

report.FilterString = "[CategoryID] = [Parameters.yourParameter1]";

最后创建好的报表可以输出给用户看了

// 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、报表设计

在报表属性中,设置默认font为微软雅黑,设置Language为默认(注意:不能设置为中文,否则导出PDF中文乱码)。

2、报表后台代码

绑定到List类型的绑定方法,设置报表上的对象的XRBinging中DataSource参数为null。

从报表的DataMember 需要设置,否则从报表只显示一条记录。

同时“计算”字段的表达式的使用。

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、调用报表

List list = new List<MyEntity> { entity };
MyReport myReport = new MyReport(); //报表实例
myReport.DataSource = list;//绑定报表的数据源
myReport.ShowPreview();

到此这篇关于.Net报表开发控件XtraReport的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • WPF框架Prism中View Injection用法介绍

    WPF框架Prism中View Injection用法介绍

    这篇文章介绍了WPF框架Prism中View Injection的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • ORM框架之Dapper简介和性能测试

    ORM框架之Dapper简介和性能测试

    这篇文章介绍了ORM框架之Dapper简介和性能测试,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • asp.net Webconfig中的一些配置

    asp.net Webconfig中的一些配置

    除了手动编辑此文件以外,您还可以使用Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“Asp.Net 配置”选项。
    2010-07-07
  • ASP.NET Core Razor页面用法介绍

    ASP.NET Core Razor页面用法介绍

    这篇文章介绍了ASP.NET Core Razor页面的用法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • asp.net FindControl方法误区和解析

    asp.net FindControl方法误区和解析

    在ASP.NET中Control都有一个FindControl方法,其作用是根据ID(注意既不是UniqueID也不是ClientID)在Control所在的命名容器中寻找相应控件,但实际使用中存在很多误区和陷阱,下面谈谈个人对此的理解
    2012-01-01
  • C#使用正则表达式实例

    C#使用正则表达式实例

    正则表达式(regular expression)是用来快速、高效地处理文本数据的工具。被处理的文本可以小到一个电子邮件地址,也可以大到一个多行文本输入框中的文本数据。正则表达式不仅可用来确认一段文本是否与一个预定义的模式相匹配,还可以用于从文本中抽取符合某一模式的数据。
    2008-04-04
  • .Net行为型设计模式之备忘录模式(Memento)

    .Net行为型设计模式之备忘录模式(Memento)

    这篇文章介绍了.Net行为型设计模式之备忘录模式(Memento),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • ASP.NET 中的Application详解

    ASP.NET 中的Application详解

    Application对象是HttpApplicationState类的一个实例,Application状态是整个应用程序全局的。本文主要详细介绍Application对象的用法。
    2016-04-04
  • Asp.net请求处理之管道处理介绍

    Asp.net请求处理之管道处理介绍

    在了解Asp.net请求处理流程的过程中,个人认为有必要从源代码的角度来了解asp.net管道是怎么实现的,需要的朋友可以参考下
    2012-11-11
  • asp.net平台下C#实现Socket通信

    asp.net平台下C#实现Socket通信

    这篇文章介绍了asp.net平台下C#实现Socket通信的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01

最新评论