65.9K
CodeProject 正在变化。 阅读更多。
Home

一种新的 .NET 报表方式

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (46投票s)

2006 年 8 月 14 日

CPOL

3分钟阅读

viewsIcon

421807

downloadIcon

17594

正在寻找一种免费且简单的方法来设计报表并将其添加到您的 .NET 应用程序中? 看看 MyNeoReport 库吧。

Sample Image - myneoreport_designer.jpg

引言

大约三年前,我开始使用著名的免费编辑器进行 .NET 开发,我需要向我的应用程序添加一些报表。由于我喜欢看到我将要打印的内容,只是不想浪费半棵树的纸来调整文本字体,所以我开始开发一个小型的报表库和一个使用它的设计器。顺便说一句,我的初始工作被包含在该著名的免费 .NET 编辑器中,但我放弃了那个项目,因为我发现它设计得很糟糕(这是大多数初稿的命运),所以它变成了一个全新的项目:MyNeoReport 引擎和设计器,这让我能够以 WYSIWYG 方式设计报表,如果我愿意,我现在可以完全控制我的应用程序中的每个报表对象。

背景

如今,如果您是业余爱好者,您也有一些 Microsoft® 免费解决方案来开发您的应用程序,但这些入门级 IDE 仍然没有内置的报表工具(而且我也不喜欢专业版中包含的那个)。 还没有一个免费的(好的和可用的)解决方案可以让您以简单的方式在托管代码中管理打印,所以我决定开始 MyNeoReport,现在,三年后,它已经相当成熟了(好吧,没有超高级的功能,但是,伙计...它使用简单,组织良好,而且免费)。

Using the Code

在本文中,我将描述一些您可以在应用程序中用于管理报表内容的代码。 请注意,您可以在运行时访问报表,也可以在设计器中定义报表。 请注意,完整的设计器几乎涵盖了引擎的所有功能,如果您愿意,您可以编写自己的设计器(可能比我的更好)。

示例文档的预览如下所示

Preview

该引擎由两个库组成

  1. NeoDataType.MyNeoReport.dll - 所有内容的核心
  2. NeoDataType.LiveCodeEngine.dll - 脚本引擎功能

这些库必须与您的应用程序一起分发,但您只需要引用 NeoDataType.MyNeoReport.dll,因为另一个只是用于支持。 您需要包含的主要 namespaceNeoDataType.MyNeoReport

// c#
using NeoDataType.MyNeoReport;

' vb.net
Imports NeoDataType.MyNeoReport

在这里您可以找到所有报表实体类。 最重要的是

  1. Report,当然,最重要的
  2. Page,它定义了页面特征
  3. Section,它标识一个部分(报表和页面页眉和页脚、详细信息和组)
  4. Label, Image, Shape, DataLabel, Dataimage,这些是您可以打印的基本项目

Report 标识报表文档,它允许您将报表加载并保存到磁盘,它为您提供了一些信息,例如报表将打印多少页,但更重要的是,它公开了 Page 属性。

Page 描述页面大小、方向、度量单位,当然还有要打印的部分。

Section 是一个水平带,包含要打印的项目。 默认部分为 ReportHeaderPageHeaderDetailsPageFooterReporFooter,但 MyNeoReport 也支持分组部分。

总而言之,报表的结构如下

 Report
  +- Page
      +- Sections
          +-Items

创建一个新报表并添加项目

// c#
private void CreateANewReportSample()
{
    // Create a new report
    Report report = new Report();
    
    // Now you can set Page properties
    Page page = report.Page;
    
    // Set the page width to 5x8 inches
    page.Units = ScaleMode.Inches;
    page.Width = 5;
    page.Height = 8;
    
    // Add some items to print
    // - add a label that indicates the page number
    Label label = page.ReportHeader.AddLabel();
    
    // This label uses special functions as described here:
    // http://www.devbox4.net/?q=node/34
    label.Text = "Page {@PageNumber} of {@PageCount}";

    // - add an ellipse shape rotated by 20 degrees
    // This is another way to create and add an item
    Shape shape = new Shape();
    shape.ShapeType = ShapeType.Ellipse;
    shape.Angle = 20;
    page.ReportHeader.AddItem(shape);
    
    // If you use the PageControl to add designer capability
    // to your application, you need to assign the Page object
    // to it
    pageControl.SetPage(report.Page);
}

定义详细信息数据源

// c#
private void SetDetailsDataSource()
{
    // Default datasource is OleDbDataSource that lets you connect to OleDb providers.
    // You can choose to pass data to a datasource changing it to a TableDataSource
    // with section.ChangeDatasourceType(DataSourceType.UseDataTable)
    OleDbDataSource dataSource = (OleDbDataSource)report.Page.Details.DataSource;
    dataSource.ConnectionString = myConnectionString;
    
    // Add a label and a data label bounded to the data
    // to the details section
    // - add a label that indicate the record number
    Label label = page.ReportHeader.AddLabel();
    
    // This label uses special functions as described here:
    // http://www.devbox4.net/?q=node/34
    label.Text = "Product {@RecordNumber} of {@RecordCount}";

    // - add a data label that indicates product name
    DataLabel datalabel = page.Details.AddDataLabel();
    // Move the data label to the right
    datalabel.X = label.X + label.Width;
    datalabel.DataField = "ProductName";
    
    // - add a data label that indicates product price
    datalabel2 = page.Details.AddDataLabel();
    // Move the data label to the right
    datalabel2.X = datalabel.X + datalabel.Width;
    datalabel2.DataField = "UnitPrice";
    // Set the text in case the data is NULL
    datalabel2.NullText = "Price undefined";
    // Set the format of the data, according to MSDN 
    // for the format strings
    datalabel2.Format = "#,###.00";
    
    // You can call the standard connection string wizard
    // using dataSource.ShowConnectionStringWizard();

}

显示预览并打印

// c#
private void ShowThePreview()
{
    // Ok, now that you set all the report properties and items
    // you can preview and print it!
    
    // First of all, you need to choose the destination printer.
    // This is needed also for the preview, so it can be appropriate.
    report.ShowPrinterDialog();

    // Show the preview in a separate window.
    // Note that you can use the NeoDataType.MyNeoReport.PreviewControl
    // if you want a preview in your form.
    report.ShowPreview();
    
    // You can also print the report directly with
    // report.Print();
    
}

关注点

我喜欢 MyNeoReport,因为它的类允许您创建一个报表(有数据绑定或没有数据绑定),无需设计器,只需通过您的代码即可,因为这些类允许您完全控制报表及其组件的每个方面。

如果您认为内置项目 (LabelImageShapeDataLabelDataImage) 太少,您必须知道 MyNeooReport 是可扩展的:您只需继承自 NeoDataType.MyNeoReport.ItemBase 即可编写您自己的项目。 您可以查看提供的 Designer 代码以检查 Report 类的所有功能。

这只是对 MyNeoReport 库功能的一个小小的介绍。 有关更多信息、文档、支持和新版本,您可以访问 支持我的站点

历史

MyNeoReport 1.4 是一个重大更新,具有一些新的增强功能、优化和错误修复。 这在下载包中包含的 upgradeinfo.txt 文件中进行了描述。

© . All rights reserved.