Web Reporting
ASP.NET WebForms
The simplest way to add reporting to an ASP.NET application is to add a "Generic Handler (.ashx)" to your project. If you are unfamiliar with .ashx files, they are basic HTTP handlers like .aspx, but they do not have the overhead of building a Page object
and a control heirarchy. They are for serving any kind of content over the web, including HTML, binary data like PDF, images, etc. They are accessed via URL just like a regular .aspx, and can be passed querystrings the same way.
WebReport will handle all serving of the report for you. It will determine what type of report to send based on the?ReportType querystring.
- PDF: http://myserver.com/Reports/ProductsReport.ashx?ReportType=PDF
- Excel: http://myserver.com/Reports/ProductsReport.ashx?ReportType=Excel
Configuration
Check out the
Configuring DoddleReport documentation to map the ReportType parameter to specific Report Writers.
When you create your ashx you can replace all the code inside as follows, you just need to implement a few basic abstract members.
publicclass ProductsReport : WebReport
{publicoverridestring FileName
{get { return"ProductsReport"; }
}publicoverride IReportSource ReportSource
{get
{// LINQ queryvar query = from p in db.Productsselect p;return query.ToReportSource();
}
}publicoverridevoid CustomizeReport(Report report)
{// Text Fields
report.TextFields.Title = "Products Report";
report.TextFields.Footer = "Copyright 2009 © The Doddle Project";
report.TextFields.Header = string.Format("Report Generated: {0}", DateTime.Now);// Data Fields
report.DataFields["ProductID"].Hidden = true;
report.DataFields["CategoryID"].Hidden = true;
report.DataFields["SupplierID"].Hidden = true;
}
}
ASP.NET MVC
And for my MVC friends I have provided a ReportResult to use as an Action Result that will serve up the report.
The ReportResult will actually read the extension of the request on the controller and return the correct type of report.
- PDF - http://myserver.com/reports/ProductsReport.pdf
- Excel - http://myserver.com/reports/ProductsReport.xls
- HTML - http://myserver.com/reports/ProductsReport.html
- Delimited - http://myserver.com/reports/ProductsReport.txt
Configuration
Check out the
Configuring DoddleReport documentation to map the URL extension with a specific Report Writer.
IMPORTANT ROUTING STEP
DoddleReport can automatically determine which IReportWriter to use based on the file extension of the route. To enable this, you must do the following:
// Add using DoddleReport.Web;
// put this line at the _top_ of your route configuration
routes.MapReportingRoute();
Next simply create your report like you normally would and return it inside an instance of ReportResult. DoddleReport will handle the rest.
public ReportResult ProductReport()
{// Get the data for the report (any IEnumerable will work)var query = ProductRepository.GetAll();var totalProducts = query.Count;var totalOrders = query.Sum(p => p.OrderCount);// Create the report and turn our query into a ReportSourcevar report = new Report(query.ToReportSource());// Customize the Text Fieldsreport.TextFields.Title = "Products Report";
report.TextFields.SubTitle = "This is a sample report showing how Doddle Report works";
report.TextFields.Footer = "Copyright 2011 © The Doddle Project";
report.TextFields.Header = string.Format(@"
Report Generated: {0}
Total Products: {1}
Total Orders: {2}
Total Sales: {3:c}", DateTime.Now, totalProducts, totalOrders, totalProducts * totalOrders);// Render hints allow you to pass additional hints to the reports as they are being renderedreport.RenderHints.BooleanCheckboxes = true;// Customize the data fieldsreport.DataFields["Id"].Hidden = true;
report.DataFields["Price"].DataFormatString = "{0:c}";
report.DataFields["LastPurchase"].DataFormatString = "{0:d}";// Return the ReportResult
// the type of report that is rendered will be determined by the extension in the URL (.pdf, .xls, .html, etc)return new ReportResult(report);
}