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

HTML报表引擎

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (76投票s)

2006年6月1日

CPOL

5分钟阅读

viewsIcon

842336

downloadIcon

23374

HTML 报表引擎是一个 .NET 类库,用于生成格式良好的 HTML 报表。

示例 HTML 报表

Generated HTML Report

HTML 报表生成器 - 示例应用程序截图

Demo Application Screenshot

引言

HTML 报表引擎是一个 .NET 类库,用于生成格式良好的 HTML 报表。它可以接收任何 DataSet 作为其报表源。报表可以包含分段数据内容和条形图。

背景

Crystal Reports 是 VS.NET 附带的报表工具,但在应用程序中设计和部署 Crystal Reports 报表有些复杂。虽然这个 HTML 报表引擎实用工具只有有限的功能,但它可以生成出色的报表,而无需复杂的设计和编码工作。

使用代码

它是一个通用的报表实用工具,以 DataSet(包含至少一个 DataTable)作为报表源。该类库包含三个主要类

  • 字段
  • 报表

Field 类包含报表字段信息,例如报表列名、列标题、背景颜色等。

Section 类包含有关节的信息,例如“分组依据”列名、标题前缀、背景颜色、图表信息等。

Report 类包含渲染 HTML 报表所需的所有方法和属性。在此类中,使用了一些 CSS(层叠样式表)类来格式化报表内容。以下代码示例说明了如何在您的应用程序中使用此实用工具

//Include the namespace
using HTMLReportEngine;

//Create report object and set properties.
Report report = new Report();
report.ReportTitle = "Issue Report";
//Create a DataSet ds and fill it before using this code.
report.ReportSource = ds;

//Create Section
Section release = new Section("Release","Release: ");

//Create SubSection
Section project = new Section("Project","ProjectID: ");

//Add the sections to the report
release.SubSection = project;
report.Sections.Add(release);

//Add report fields to the report object.
report.ReportFields.Add(new Field("TicketNo", "Ticket", 50, ALIGN.RIGHT));
report.ReportFields.Add(new Field("CreatedBy", "CreatedBy", 150));
report.ReportFields.Add(new Field("AssignedTo", "AssignedTo"));
report.ReportFields.Add(new Field("Release", "Release", 200));
report.ReportFields.Add(new Field("Project", "Project", 150, ALIGN.RIGHT));

//Generate and save the report
report.SaveReport(@"C:\Data\Report.htm");

类属性和方法

字段
属性 类型 描述
FieldName 字符串 DataSet 中的列名。
HeaderName 字符串 将显示为列标题的字符串。
宽度 Int 列宽(以像素为单位)。
对齐 ALIGN 字段的文本对齐方式。默认为 ALIGN.LEFT
BackColor Color 列背景色。
HeaderBackColor Color 列标题背景色。
isTotalField bool 如果为 true,则该字段包含在总计中。默认为 false
Field() 构造函数 有八个重载构造函数可用。
属性 类型 描述
GroupBy 字符串 要应用“分组依据”的列名。
TitlePrefix 字符串 节标题的前缀文本。
BackColor Color 节标题背景色。
GradientBackground bool true – 显示渐变色;false – 显示纯色背景。默认为 false
IncludeTotal bool 显示/隐藏总计行。
SubSection 类型为 Section 的子节。
IncludeChart bool 显示/隐藏图表。
ChartTitle 字符串 图表标题文本。
ChartShowAtBottom bool true - 在底部显示图表(在数据行之后);false – 在顶部显示图表。默认为 false
ChartChangeOnField 字符串 图表的 Y 轴字段,通常是文本字段。
ChartValueField 字符串 图表的 X 轴字段,必须是数字字段。默认为记录计数。
ChartShowBorder bool 启用/禁用图表边框。
ChartLabelHeader 字符串 图表标签列标题文本。默认为“Label”。
ChartPercentageHeader 字符串 图表百分比列标题文本。默认为“Percentage”。
ChartValueHeader 字符串 图表值列标题文本。默认为“Value”。
Section() 构造函数 有三个重载构造函数可用。
报表
属性 类型 描述
ReportTitle 字符串 报表标题文本。
ReportSource DataSet 报表源是 DataSetDataSet 必须包含至少一个包含数据的 DataTable
ArrayList 报表节的集合。每个元素都是 Section 类型。
ReportFields ArrayList 报表字段的集合。每个元素都是 Field 类型。
ReportFont 字符串 报表字体,为 string 类型。
TotalFields ArrayList 将列在总计行中的列名集合。每个元素都是 string 类型。
IncludeTotal bool 显示/隐藏总计行。
IncludeChart bool 显示/隐藏图表。
ChartTitle 字符串 图表标题文本。
ChartShowAtBottom bool true - 在底部显示图表(在数据行之后);false – 在顶部显示图表。默认为 false
ChartChangeOnField 字符串 图表的 Y 轴字段,通常是文本字段。
ChartValueField 字符串 图表的 X 轴字段,必须是数字字段。默认为记录计数。
ChartShowBorder bool 启用/禁用图表边框。
ChartLabelHeader 字符串 图表标签列标题文本。默认为“Label”。
ChartPercentageHeader 字符串 图表百分比列标题文本。默认为“Percentage”。
ChartValueHeader 字符串 图表值列标题文本。默认为“Value”。
GenerateReport() 方法 生成 HTML 报表并将其作为 StringBuilder 对象返回。
SaveReport(string fileName) 方法 生成 HTML 报表并将其保存到指定文件名的磁盘。

格式化报表

包含总计

此报表引擎支持多个 SUM 字段的报表。SUM 可以在添加到报表的任何节中按需显示。SUM 字段必须是数字类型。以下代码解释了如何添加 SUM 字段

//Add Total fields
report.TotalFields.Add("Project");
report.TotalFields.Add("ProgressedHours");

//Set IncludeTotal property of the sections to true
project.IncludeTotal = true;
release.IncludeTotal = true;

Total Fields Sample

添加图表

此引擎可以生成条形图。开发人员只需提供 ChangeOnFieldValueField 作为输入。如果开发人员未提供任何 ValueField,则记录计数将作为 Value。更改 ChartShowAtBottom 属性可让您的图表显示在节的顶部或底部。

//Set Chart properties
release.ChartChangeOnField = "Severity";
release.ChartValueField = "ProgressedHours";

release.ChartTitle = "Severity-Wise report";
release.ChartLabelHeader = "Severity Type";
release.ChartValueHeader = "Hours";
release.ChartPercentageHeader = "Progress";

对齐字段

字段文本可以对齐到 LeftRightCenter。默认情况下,文本对齐设置为 Left

//Set field Alignment
Field field1 = new Field();
field1.Alignment = ALIGN.RIGHT;

指定颜色

允许开发人员更改节标题、列标题和列数据的背景颜色。通过将 GradientBackground 属性设置为 true,节标题可以具有渐变背景。

//Specifying Colors
//Main Section header color
release.BackColor = Color.WhiteSmoke;
release.GradientBackground = true;

//Sub Section header color
project.BackColor = Color.GhostWhite;
project.GradientBackground = true;

//Field Colors
field1.HeaderBackColor = Color.LightSlateGray;
field1.BackColor = Color.Gainsboro;
field2.HeaderBackColor = Color.LightSlateGray;
field2.BackColor = Color.White;
field3.HeaderBackColor = Color.LightSlateGray;
field3.BackColor = Color.Gainsboro;

关注点

建议为除一个字段外的所有报表字段指定列宽。一个字段可以是可变文本字段。没有指定列宽的字段将自动适应表格中剩余的空间。

有一个简单的方法可以将数据导出到 MS Excel。您可以将生成的 HTML 文件在 Internet Explorer 中打开,右键单击报表,然后选择“导出到 Excel”。搞定!

历史

  • 版本 2.0 - 添加了格式化功能,并允许报表中包含多个总计列。
  • 版本 1.0 - 初始版本。
© . All rights reserved.