1

I know there is a lot of similar question but I still cannot find a solution,

my code:

    resp.setContentType("text/csv; charset=UTF-8");
    resp.addHeader("Content-Disposition", "attachment; filename=file.csv");
    resp.addHeader("Pragma", "no-cache");
    resp.addHeader("Expires", "0");
    resp.addHeader("Content-Encoding", "UTF-8");
    resp.getWriter().write(sb.toString());

The result is opened as a gibrish text instead of hebrew in excell.

How should I change this text so the result CSV can be opened in Excell (without importing it to excell)?

i.e. what charset should I used, and what is the correct code to generate it?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Elia Weiss
  • 8,324
  • 13
  • 70
  • 110

2 Answers2

4

Do

sb.insert(0, '\uFEFF');
resp.getWriter().write(sb.toString());

This inserts a BOM character, a zero-width space, to mark the text as Unicode. It is redundant, even ugly. But in this way Excel & Notepad won't mistake the encoding for the platform encoding.

Caveat:

Now cell A1 cannot contain a number.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
2

Unfortunately the encoding headers in your response won't be associated with the attachment; they're only for use when reading the response body.

Excel is oddly bad at handling UTF-8 encoded CSV files. Here's a good answer from the past on convincing it to behave; the other option would be to re-encode your CSV in Windows-1252.

Community
  • 1
  • 1
Erin Call
  • 1,764
  • 11
  • 15