Click here to Skip to main content
15,888,454 members
Articles / Web Development / HTML
Article

The HTML Report Engine

Rate me:
Please Sign up or sign in to vote.
4.94/5 (79 votes)
23 Jun 2006CPOL5 min read 829.1K   23.3K   277   62
The HTML report engine is a .NET class library which helps in generating well formatted HTML reports.

Sample HTML Report

Generated HTML Report

HTML Report Generator - Sample Application Screenshot

Demo Application Screenshot

Introduction

The HTML Report Engine is a .NET class library which helps in generating well formatted HTML reports. It can take any DataSet as its report source. Reports can have sectional data contents and bar charts.

Background

Crystal Reports is a reporting tool that comes along with VS.NET, but designing and deploying a Crystal Reports report in your application is a bit complex. Though this HTML report engine utility has only limited number of features, it can produce awesome reports with no complex designing and coding work involved.

Using the code

It’s a generic reporting utility which takes as a report source, a DataSet containing at least one DataTable. The class library contains three main classes:

  • Field
  • Section
  • Report

The Field class contains report field information like report column name, column header, background color, and so on.

The Section class holds information about a section like ‘group by’ column name, title prefix, background color, charting information, and so on.

The Report class contains all the methods and properties required to render an HTML report. In this class, a few CSS (Cascaded Style Sheet) classes are used to format the report content. The following code sample explains how to use this utility in your application:

C#
//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");

Class properties and methods

Field
PropertyTypeDescription
FieldNameStringA column name in the DataSet.
HeaderNameStringString to be displayed as column header.
WidthIntWidth of the column in pixels.
AlignmentALIGNText alignment of the field. Default is ALIGN.LEFT.
BackColorColorColumn background color.
HeaderBackColorColorColumn header background color.
isTotalFieldboolIf true, the field is included for total. Default is false.
Field()ConstructorThere are eight overloaded constructors available.
Section
PropertyTypeDescription
GroupByStringColumn name on which group by is to be applied.
TitlePrefixStringPrefix text for section header.
BackColorColorSection header background color.
GradientBackgroundbooltrue – to display gradient color; false – to display plain background color. Default is false.
IncludeTotalboolDisplay/hide Total row.
SubSectionSectionSubsection of type Section.
IncludeChartboolDisplay/hide chart.
ChartTitleStringChart title text.
ChartShowAtBottombooltrue - show chart at bottom (after data rows); false – show chart at top. Default is false.
ChartChangeOnFieldStringChart Y-axis field, usually text field.
ChartValueFieldStringChart X-axis field, must be a numeric field. Default is record count.
ChartShowBorderboolEnable/disable chart border.
ChartLabelHeaderStringChart label column header text. Default is ‘Label’.
ChartPercentageHeaderStringChart percentage column header text. Default is ‘Percentage’.
ChartValueHeaderStringChart value column header text. Default is ‘Value’.
Section()ConstructorThere are three overloaded constructors available.
Report
PropertyTypeDescription
ReportTitleStringReport title text.
ReportSourceDataSetReport source is a DataSet. The DataSet must contain at least one DataTable with data.
SectionsArrayListCollection of report sections. Each element is of type Section.
ReportFieldsArrayListCollection of report fields. Each element is of type Field.
ReportFontStringReport font as string.
TotalFieldsArrayListCollection of column names to be listed in Total row. Each element is of type string.
IncludeTotalboolDisplay/hide Total row.
IncludeChartboolDisplay/hide chart.
ChartTitleStringChart title text.
ChartShowAtBottombooltrue - show chart at bottom (after data rows); false – show chart at top. Default is false.
ChartChangeOnFieldStringChart Y-axis field, usually text field.
ChartValueFieldStringChart X-axis field, must be a numeric field. Default is record count.
ChartShowBorderboolEnable/disable chart border.
ChartLabelHeaderStringChart label column header text. Default is ‘Label’.
ChartPercentageHeaderStringChart percentage column header text. Default is ‘Percentage’.
ChartValueHeaderStringChart value column header text. Default is ‘Value’.
GenerateReport()MethodGenerates the HTML report and returns it as a StringBuilder object.
SaveReport(string fileName)MethodGenerates the HTML report and saves it to disk with the given file name.

Formatting the report

Including totals

This report engine supports multiple SUM fields for a report. And the SUM can be displayed on demand in any of the sections added to the report. The SUM field must be of type numeric. The following code explains how to add SUM fields:

C#
//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

Adding charts

This engine can produce bar charts. The developer has to provide only the ChangeOnField and ValueField as input. If the developer has not given any ValueField, the record count will be taken as the Value. Changing the ChartShowAtBottom property would let your charts come either at the top or the bottom of the section.

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

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

Aligning fields

Field texts can be aligned to Left, Right, or Center. By default, the text alignment is set to Left.

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

Specifying colors

The developers are allowed to change the background colors of section headers, column headers, and column data. Section headers can have gradient backgrounds by setting the GradientBackground property to true.

C#
//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;

Points of interest

It is recommended that you specify the column width for all report fields except one. That one may be a variable text field. The field without column width will automatically fit into the remaining space available in the table.

There is a simple way to export data to MS Excel. You can open the generated HTML file in Internet Explorer, right click on the report, and select 'Export to Excel'. Done!

History

  • Version 2.0 - Added features for formatting and to allow multiple Total columns in the report.
  • Version 1.0 - Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BNY Mellon
India India
Ambalavanar, working as a .NET Solutions Architect at BNY Mellon (iNautix), Chennai. Enjoys designing and developing UI & highly scalable apps.

Comments and Discussions

 
GeneralAwsome Article Pin
rmarkram24-Oct-08 1:13
rmarkram24-Oct-08 1:13 
GeneralChart on report section Pin
Watever4422-Aug-08 11:46
Watever4422-Aug-08 11:46 
GeneralRe: Chart on report section Pin
Watever4422-Aug-08 11:56
Watever4422-Aug-08 11:56 
GeneralRe: Chart on report section Pin
Ambalavanar Thirugnanam22-Aug-08 21:12
Ambalavanar Thirugnanam22-Aug-08 21:12 
QuestionMulti-page reports? Pin
mlyons1-Jul-08 19:30
mlyons1-Jul-08 19:30 
AnswerHtml report Pin
Member 285513523-May-08 18:10
Member 285513523-May-08 18:10 
GeneralVery Innovative! Pin
Illogic1-Oct-07 18:16
Illogic1-Oct-07 18:16 
GeneralATGrid Report Control Pin
etcell30-Jul-07 18:42
etcell30-Jul-07 18:42 
ATGrid报表控件简介
下载地址:
http://www.etcell.com/
http://www.etcell.com/download.aspx?id=51

ATGrid报表控件是在ETCell报表控件基础上推出的第二代报表控件,是一款完全对象化的报表组件。
ATGrid在功能上相对ETCell做出了重大改善:
支持多个ETSet数据集
支持7个子对象,最小对象到单元格
支持33种单元格数据类型
支持任意复杂格式报表数据展现
支持23种图表展示
扩展xml支持,操作数据更加灵活方便
改进打印功能,更好的支持套打、连续打印、分页打印
提供了ASP、ASP.NET、JSP、PHP专用类库组件,提高开发效率

立即试用ATGrid报表控件




示例演示&源码下载




ATGrid报表控件功能
1、设计报表式样,生成模板文件
专用报表模板设计器,可视化设计报表式样

ATGrid报表控件有专门的设计器——wintable报表设计器,用来设计报表模板。wintable采用所见即所得的设计模式,类Excel操作界面风格,易学易用。可以直接导入Excel电子表格文件,也可以把ATGrid报表导出成Excel文件、html文件、xml文件。


支持分组、交叉、分栏等复杂报表

ATGrid报表控件采用表格方式构建报表,方便灵活,数据模型先进,可支持多个变长数据集,支持横纵向扩展,支持分组、交叉、分栏等复杂报表。


支持23种图表展现方式

ATGrid报表控件支持饼图、折线图、柱状图等23种图表展现方式,在一张报表中既可以有报表又可以有图表,大大丰富了报表数据的展现方式,增加了报表的可读性。


支持按钮、下拉列表、日期、多行文本等多种控件效果,支持会计表头、表览等多种特殊效果

ATGrid报表控件提供了33种单元格数据类型,包括数字、文本、密码、按钮、日期、列表等等,还包括会计表头、表览等多种特殊类型。使用ATGrid可以展现复杂的报表式样。


2、数据操作
拥有规范的xml数据格式

ATGrid报表控件提供xml数据接口,支持符合ATGrid数据格式规范的xml数据。ATGrid报表控件数据xml格式兼容ETcell报表控件数据格式。


内置通讯接口,强力支持HTTP协议,可以和后台程序轻松交互

ATGrid报表控件提供内置通讯接口,可以以post或get方式发送表单内数据或xml数据,可以接收返回的数据。为报表数据与其他程序交互操作提供了良好的支持,实现了数据与式样的分离。


采用Alias别名技术,可单独对单元格数据进行操作

ATGrid报表控件可以通过别名对单元格数据进行操作,大大增加了程序的灵活性和可扩展性。


数据模型先进,支持两种数据集——AliasSet和ETSet

ATGrid报表控件支持两种数据集模型,一种是AliasSet,相当于一条记录;一种是ETSet,相当于多条记录集。在ATGrid报表控件中可以直接对数据集进行操作,提高了编程的方便性。


ATGrid报表控件支持公式

ATGrid报表控件支持和Excel兼容的公式,单元格间可以自动进行计算。


3、编程接口控制
支持多种开发语言

支持多种常见开发语言,如JSP、ASP、ASP.NET、C#、PHP、VB、VC++、Delphi、C++Builder、PowerBuilder、Java等。


支持VBScript、JavaScript脚本

使用JavaScript或VBScript进行web编程,可以轻松实现浏览器端人机交互效果。


对象化编程

ATGrid是一款完全面向对象的报表控件,符合面向对象编程思想,支持7个子对象,最小对象到单元格,大大方便了面向对象编程。


提供数百个开发接口

ATGrid提供数百个开发接口,可以灵活的对式样、数据、事件进行控制。


4、打印输出
打印设置

可以按照打印效果的需要进行灵活的设置,包括页边距、纸张大小、打印方向、打印预览界面自定义、打印比例大小、表格线是否打印、是否自适应纸张、是否打印报表背景颜色、是否打印单元格背景,是否分页打印。


报表套打

可以按照预定格式,设置报表表格线是否打印、单元格是否打印,满足套打需要。


分页打印

可以设置固定表头等多种特殊效果,当数据过多超出一页纸时,可以进行分页打印。


自适应纸张打印

可以让报表自动缩放到符合纸张大小,充满整张纸,自动适应纸张进行打印。


导出其它格式文件

ATGrid报表可以导出Excel、html、xml等多种文件格式。导出为Excel文件时,报表内的公式、数据、式样会自动随着导出。

下载地址:
http://www.etcell.com/
http://www.etcell.com/download.aspx?id=51



GeneralOrder of sections Pin
JACK0245926-Jun-07 5:18
JACK0245926-Jun-07 5:18 
QuestionNice work...but I have 1 question Pin
bcpdrp25-Jun-07 13:22
bcpdrp25-Jun-07 13:22 
AnswerRe: Nice work...but I have 1 question [modified] Pin
Member 222654227-Sep-08 4:31
Member 222654227-Sep-08 4:31 
GeneralHTML & Unicode Pin
César de Souza19-Jan-07 15:25
professionalCésar de Souza19-Jan-07 15:25 
GeneralNice innovation Pin
Ganesan Senthilvel29-Nov-06 22:44
Ganesan Senthilvel29-Nov-06 22:44 
GeneralReport Definition Pin
graham.lock6-Sep-06 22:44
graham.lock6-Sep-06 22:44 
GeneralHtml Report Pin
Terryjsz14-Jul-06 4:37
Terryjsz14-Jul-06 4:37 
GeneralGood Job Pin
Michael A. Barnhart15-Jun-06 7:00
Michael A. Barnhart15-Jun-06 7:00 
GeneralGood Job Pin
AshimPokharel14-Jun-06 3:33
AshimPokharel14-Jun-06 3:33 
GeneralDesign issues Pin
NinjaCross13-Jun-06 21:43
NinjaCross13-Jun-06 21:43 
GeneralRe: Design issues Pin
Ambalavanar Thirugnanam14-Jun-06 0:06
Ambalavanar Thirugnanam14-Jun-06 0:06 
GeneralFantastic Idea Pin
James Lawrence5-Jun-06 22:32
James Lawrence5-Jun-06 22:32 
GeneralSmall suggestion Pin
Rei Miyasaka1-Jun-06 22:38
Rei Miyasaka1-Jun-06 22:38 
GeneralRe: Small suggestion Pin
Ambalavanar Thirugnanam13-Jun-06 17:59
Ambalavanar Thirugnanam13-Jun-06 17:59 
JokeGood Work!! Pin
SurenSoman1-Jun-06 19:44
SurenSoman1-Jun-06 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.