$ grails
Enter a command name to run. Use TAB for completion:e...
grails> create-controller Book
| Created grails-app/controllers/csv/BookController.groovy
| Created src/test/groovy/csv/BookControllerSpec.groovy

A) I will recommend to put the logic fetching the lines in a Service.
B) I am using the mime type and encoding defined in application.yml. Learn more about retrieving config values.
C) If you want the file to download you need to setup the Content-disposition header.

If we run the app and call the controller we will download a CSV file as this:

CSV is probably the best format to export your data from a Grails App. A CSV file is easy to import in Excel. I was tired of my clients asking me how to import a CSV in Excel. I wrote a post; in Spanish though.

Do you like to read about Grails / Groovy development? If the answer is yes, subscribe to Groovy Calamari. A weekly curated email newsletter about the Groovy ecosystem. Curated by me 🎉