I have a 3-tier web application (database, app tier, web tier) written in MVC.
I want to generate Excel files at runtime using the data from the database.
My initial idea is to reference Excel Library in the web project and generate the file in the controller. I can stream it directly to the response and I can treat the file as a different view of the data. I already have a service method in place that returns the data from the database, as it is used on a page.
I am considering file generation on the app tier and then returning byte[] to web tier. The argument here is that the file generation is part of business logic.
What would you do?
The excel output is actually a responsibility of the view, so you MUST do this in the controller. The model/app tier is responsible for data only, not the representation of it.
If you are using ASP.Net MVC you can declare an ExcelActionResult which takes in the view model and transforms it to a CSV/Excel output. Some related information:
Custom Excel Export Action
http://stephenwalther.com/blog/archive/2008/06/16/asp-net-mvc-tip-2-create-a-custom-action-result-that-returns-microsoft-excel-documents.aspx