Maybe you want to consider the Forena module (disclosure: I'm a co-maintainer of it) ... Here is a quote from that page:
... built of the idea of using SQL to get data out of a database and use XHTML and CSS to format it into web reports.
Here is another quote from Reasons for considering Forena, related to the "CSV" in the question here:
Reports created by Forena (combined with SVGGraph and mPDF) can be saved (exported) as PDF documents with embedded graphs. Other supported formats for saving a report are CSV, XLS, DOC, HTML, etc.
My answer to "How to retrieve data directly from a Drupal database" contains like a 1 minute summary to explain the typical (about 3) steps for creating:
- a connection to an external database (typically by an admin).
- a custom SQL query (typically by an admin).
- a custom report (either by an admin, or by just any regular user who is either familiar with creating what is called 'FRX files', or otherwise using the WYSIWYG report editor).
These steps (without any PHP coding involved ...) are like the equivalent to writing the PHP coding as included in the question there.
For an online demo (prior to installing/testing Forena), refer to these links: