Sometimes, we need to perform statistical analysis on a pile of data to generate an HTML or Excel report. This is not a very difficult task, but it is a very troublesome task. The biggest headache is that when a leader encounters a temporary emergency task, the leader often does not know what the report is. Just give you a bunch of data and let you prepare an analysis report first, when you turn over the analysis report, the lead will ask you to submit another Analysis Report for the analysis results... . In this case, a quick report generation tool is very convenient.
Now, we can use DoddleReport to quickly generate reports. A simple example is as follows:
Assume that our data object is in the following format:
Public class Product
{
Public
Int Id {get; set ;}
Public
String Name {get; set ;}
Public
String Description {get; set ;}
Public
Double Price {get; set ;}
Public
Int OrderCount {get; set ;}
Public
DateTime LastPurchase {get; set ;}
Public
Int UnitsInStock {get; set ;}
}
The data source is generated using the following functions:
Static
IEnumerable <Product> GetAllData ()
{
Var rand = new
Random ();
Return
Enumerable. Range (1, 20). Select (
I => new
Product
{
Id = I,
Name = "Product" + I,
Description = "This is an example description ",
Price = rand. NextDouble () * 100,
OrderCount = rand. Next (1000 ),
LastPurchase = DateTime. Now. AddDays (rand. Next (1000 )),
UnitsInStock = rand. Next (0, 2000)
});
}
To generate a report for this data source, you only need to perform the following steps:
1. Use the ToReportSource Extension function to convert the data source to a Report object.
// Create the report and turn our query into a ReportSource
Var report = new
Report (GetAllData (). ToList (). ToReportSource ());
2. Add the report title and the description of the header and footer.
// Customize the Text Fields
Report. TextFields. Title = "Products Report ";
Report. TextFields. SubTitle = "This is a sample report showing how Doddle Report works ";
Report. TextFields. Footer = "Copyright 2011 & copy; The Doddle Project ";
Report. TextFields. Header = string. Format (@ "Report Header Demo ");
// Render hints allow you to pass additional hints to the reports as they are being rendered
Report. RenderHints. BooleanCheckboxes = true;
3. Format Control for output Fields
// Customize the data fields
Report. DataFields ["Id"]. Hidden = true;
Report. DataFields ["Price"]. DataFormatString = "{0: c }";
Report. DataFields ["LastPurchase"]. DataFormatString = "{0: d }";
4. Report output
Using (var outputStream = File. Create (@ "r: \ report.html "))
{
Var writer = new DoddleReport. Writers. HtmlReportWriter ();
Writer. WriteReport (report, outputStream );
}
In this way, we can get the following report:
It is very simple to use, but basically it is quite good to use it. Steps 2 and 3 can be omitted. In the simplest way, you only need the following lines:
// Create the report and turn our query into a ReportSource
Var report = new
Report (GetAllData (). ToList (). ToReportSource ());
Using (var outputStream = File. Create (@ "r: \ report.html "))
{
Var writer = new DoddleReport. Writers. HtmlReportWriter ();
Writer. WriteReport (report, outputStream );
}
It is worth mentioning that DoddleReport supports a wide range of output tasks: Common formats such as PDF, EXCEL, HTML, and CSV are supported, and can also be directly output to online reports in Asp.net. For example, you only need to change "Writers. HtmlReportWriter" in the above example to "OpenXml. ExcelReportWriter" to generate reports in Excel format, which is very powerful and convenient.