开发者

French, Spanish accented characters does not display correctly in Excel [duplicate]

开发者 https://www.devze.com 2023-02-03 01:09 出处:网络
This question already has answers here: Microsoft Excel mangles Diacritics in .csv files? (22 answers) Closed 2 years ago.
This question already has answers here: Microsoft Excel mangles Diacritics in .csv files? (22 answers) Closed 2 years ago.

I am trying to generate reports in different languages like French, Spanish, etc. The report is being viewed using MS Excel 2007, It displays OK in English but not in French nor in Spanish when executed in Linux environment.

All languages works in Windows Server but when running it in Linux, I am experiencing the problem I described above.

The code is in Java and, I started like this:

  String contentType = "text/csv; charset=utf-8";
  // resp is HttpServletResponse
  resp.setCharacterEncoding("utf-8");
  resp.setHeader("Cache-Control", "max-age=0,must-revalidate");
  resp.setHeader("Pragma", "cache");
  resp.setDateHeader("Last-Modified", System.currentTimeMillis());
  resp.setHeader("Content-disposition", "attachment; filename=\"Report.csv\"");
  resp.setContentType(contentType);
  OutputStream os = resp.getOutputStream();

  os.write(new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF }); 
  //os.print("\uFEFF".getBytes()); <<- tried this too, did not worked!

  //where sb is StringBuffer sb = new StringBuffer();
  //and string values were added by sb.append("\"" + someString + "\",");

  os.write(sb.toString().getBytes());

Please help and thank you in a开发者_运维技巧dvance.


You need to specify charset in your call to getBytes(). By not specifying the charset, you are telling Java to use the default encoding for whatever platform this code ran on. When opened on another platform, the result is likely wrong. You might just try getBytes( "UTF-8" )...

0

精彩评论

暂无评论...
验证码 换一张
取 消