1

in this Grails application, I'm exporting database registers to a csv file using the code:

response.setHeader "Content-disposition", "attachment; filename=creds.csv"
response.contentType = 'text/csv; charset=UTF-8'
response.setCharacterEncoding("UTF-8")
response.outputStream << respstr
response.outputStream.flush()

and the file downloaded by the browser has accents issues.

I can tell the data in the db is ok because I also implemented the generation of xls files using apache.poi.* and the accents are displayed just fine.

psql -l returns:

Name     |  Owner   | Encoding  | Collation | Ctype |
mydb     | postgres | SQL_ASCII | C         | C     |

What should I change to make it work ? Thanks

xain
  • 13,159
  • 17
  • 75
  • 119
  • 1
    I think this is exact duplicate of a question: http://stackoverflow.com/questions/4784810/problem-with-german-umlauts-in-generated-csv-file-using-grails – Victor Sergienko Jun 08 '11 at 13:49
  • Tried setting UTF-8 when importing but no luck. – xain Jun 09 '11 at 12:50
  • Sorry, setting what, BOM? Can you find out what encoding the resulting file is actually in? If it's correct UTF-8, then the problem is on Excel side. If not, then it's a database collation problem, and you must be seeing no umlauts/accents in your web UI either - do you? Where does `respstr` come from, after all? – Victor Sergienko Jun 09 '11 at 16:07

2 Answers2

2

Solution: It can be resolved by adding addition header (BOM, Byte order Mask) to the downloaded CSV file.

response.setContentType("application/vnd.ms-excel:UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=myOutputFile.csv");

// Excel does not recongize the UTF-8, add additional header (BOM) for excel
OutputStream outputStream = response.getOutputStream();
outputStream.write(0xEF);  
outputStream.write(0xBB);
outputStream.write(0xBF);   

Source: http://yp-javadev.blogspot.com/2012/04/resolved-download-csv-with-utf-8.html

hyattobane
  • 21
  • 2
0

If it's a broken DB collaction, then I recommend you to fix it on DB level - convert encoding/collation to UTF-8.

If it's not possible, you can try:

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91