Render in CSV format in grails

Grails provides a Converter class to quickly convert any Java / Groovy objects to an XML / JSON response. How,

render obj as XML 

or

 render obj as JSON 

I am working on a grails application that requires me to render an object in csv format. Is there any way to do this?

I tried a few things, and I explained below:

Snippet of my code

 csv { def results = [] for(d in Data.list()) { def r= [d.id, d.name] results << r } def result = '' results.each{ row -> row.each{ col -> result += col + ',' } result = result[0..-2] result += '\n' } println result render(contentType:'text/csv',text:result) } 

I saved my results in an ArrayList and then converted them to a comma-separated string and then passed it to the render method. When I run the above code in the browser, it creates the desired file, and the browser opens the Save As dialog box for the file.

When I changed the contentType to text / html, the contents of the file are displayed in the browser without newlines.

Is there a better way to render the contents of a csv file in a browser in the same way as in a file.

Thanks.

+4
source share
3 answers

You can display CSV as text/plain instead of text/html . Browsers will present this as plain text, similar to what you see if you open the file in an editor.

+3
source

So, I was just trying to solve the same problem - responding to a Grails 3 request with CSV download.

I got a tip from the Grails slack channel to use the Apache Commons CSV code, which takes care of details such as handling commas, quotes, and other special characters.

To use it, you need to add it depending on the project. In build.gradle , under dependencies add:

 compile "org.apache.commons:commons-csv:1.2" 

Here is an example of a controller that integrates everything and responds to all requests with CSV loading for the Item domain object.

 package name.of.package import grails.rest.* import grails.converters.* import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVPrinter class ItemController extends RestfulController { static responseFormats = ['json', 'xml', 'csv'] ItemController () { super(ApprovedHotel) } def export(Integer max) { def items = Item.list() withFormat { csv { def results = [] for(d in items) { def r= [d.column1, d.column2] results << r } StringWriter stringWriter = new StringWriter(); CSVPrinter printer = new CSVPrinter(stringWriter, CSVFormat.EXCEL); printer.printRecords( results ) printer.flush() printer.close() result = stringWriter.toString() response.setHeader("Content-disposition", "attachment; filename=filename.csv") render(contentType:'text/csv',text:result) } } } } 
+3
source

If you want to display csv data in a browser, you need to use

 <br> 

instead of \ n for newlines, if you want newlines to display correctly in HTML. Browsers usually ignore control characters and only basic formatting on HTML tags.
If you want to allow the user to download the csv file code, as shown below:

 response.setHeader("Content-disposition", "attachment; filename=Edata.csv") render(contentType: "text/csv", text:varContainingData ) 
+2
source

Source: https://habr.com/ru/post/1346111/


All Articles