法语,西班牙语重音字符无法在Excel中正确显示

时间:2011-01-04 19:36:16

标签: java csv excel-2007

我正在尝试使用法语,西班牙语等不同语言生成报告。报告正在使用MS Excel 2007查看,在Linux环境中执行时,它显示为英文,但不是法语,也不是西班牙语。

所有语言都适用于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());

请提前帮助并表示感谢。

2 个答案:

答案 0 :(得分:2)

您需要在调用getBytes()时指定charset。通过不指定charset,您告诉Java使用默认编码来运行此代码所运行的任何平台。在另一个平台上打开时,结果可能是错误的。您可以尝试使用getBytes(&#34; UTF-8&#34;)...

答案 1 :(得分:0)

这已经工作/已关闭,我找到了修复:

Microsoft Excel mangles Diacritics in .csv files?

相关问题