In some of the work I am doing I have had the need to create documents programmatically. Specifically I have to create documents that look a certain way based on dynamic data sources which may or may not need calculation before output. I considered a few options when deciding how to approach this, one possible option was to write the output using Visual Studio objects, another was to write the output using the C# drawing libraries to create the document line by line. Neither of those options had much appeal as they would take a long time to implement and would require me being a lot more comfortable with that aspect of programming than I currently am. I considered, and attempted, things like Microsoft’s reporting framework and also the VS version of CrystalReports. The output looked absolutely awful from those attempts and I felt like I needed something more flexible that I knew for certain I could manipulate, modify and contort to my needs. As an experiment I decided to translate some of my PHP skills to C#. I began writing some sample output in HTML using C# and then displayed it in the build-in web browser control. It seemed to work well. I began experimenting with using CSS to control the design of the output and that worked well also. I’m not sure it’s the best possible practice, but from my perspective it is HTML doing what HTML was designed to do. Most of my report output is tables, which has always seemed to be one of the strength’s of HTML (before the invention of the DIV and the SPAN anyway), so it was a logical choice. I also could not see the point in reinventing the wheel for this particular case since HTML is so flexible. The other encouraging thing is that MS Word can read basic HTML documents with a surprising level of accuracy to what its browser counterpart (Internet Explorer) displays. That simple capability makes it possible for the document to be manipulated after it is generated so that other elements can be added (such as graphs and charts) while still having the bulk of the data already prepared.
Writing a report in HTML may not be the first thing that comes to mind, but it is a lot cleaner, easier and overall more flexible than anything that CrystalReports could ever do. Also I found the lack of conversion options for the Crystal report objects to be a major turn off. I was encouraged several times to try Crystal, and I did, but for me it just can’t do what I need. Where it took my supervisor several hours to develop a simple multi-table report to measure survey responses, once I had my baseline report writer created it only took me a few minutes to enter the parameters for the report to have the desired output. Also, I noticed that what both Crystal and the Microsoft reporting tools refer to as a “report”, I refer to as a “section.” At best they are useful for writing a single table per page, at worst they just don’t understand and try to produce multiple pages of nothing.
There are obviously a few problems with this. Most notably, I did have to write the report writer from scratch and conceive of the entire structure that it uses to be able to make things work this easily. It has taken me a few weeks of work to build the writer to where it is, but now that I have, it is very flexible. Also, I can evolve the writer as little or as much as I need. If I were to decide later that I want it to be able to create graphs and include them, I can do that by using C#’s libraries that bridge to Excel, then pull in what I need using the drawing libraries (dumping the output to image files). Another option is to move some of the controls that I have placed in a database table to a more user-friendly control panel for editing, giving a lot more flexibility with less effort. The other problem that I’ve encountered is that at this stage it is very difficult to include graphs. I mentioned how I could from Excel, but that would require more data juggling and complex operations to accomplish. It may be possible to write them with CSS/HTML5, but that would also be a challenge of my coding skill and probably still wouldn’t work in Word.
I started writing my output as HTML as a temporary solution for what I perceived to be my lack of skill with report writers. It turns out I just have a huge distaste for report writers because they are not flexible, intuitive or adaptive. I now feel like using HTML for output might be the best for my particular situation and I think I can evolve my report writer to be perfectly suited to the needs of my organization.