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" )...
精彩评论