我试图生成不同语言的报告,例如法语,西班牙语等。正在使用MS Excel 2007查看该报告。当在Linux环境中执行该报告时,英语显示为OK,但法语和西班牙语均显示为OK。

所有语言都可以在Windows Server中使用,但是在Linux中运行它时,我遇到了上述问题。

代码是用Java编写的,我开始是这样的:

  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());


请帮助并提前感谢您。

最佳答案

您需要在对getBytes()的调用中指定字符集。通过不指定字符集,您是在告诉Java将默认代码用于运行此代码的任何平台。在另一个平台上打开时,结果可能是错误的。您可能只是尝试getBytes(“ UTF-8”)...

关于java - 法语,西类牙语重音字符无法在Excel中正确显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4597607/

10-11 22:34
查看更多