开发者

CSV file with japanese characters not opening properly in Excel 2007

I have a simple JSP to download a csv file containing Japanese characters. Downloaded file opens correctly in notepad++ and shows right characters but when I try to open same file in Excel 2007, It shows some different characters, probably in some other encoding.

My JSP looks like this.

<%@ page language="java" contentType="text/csv; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<%  
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/csv; charset=UTF-8");
    response.addHeader("Content-Disposition", "attachment; filename= " + "Test.csv");
    OutputStream output = response.getOutputStream();   
    System.out.println("Chaaracter encoding : " + response.getCharacterEncoding()); 
    Writer writer = new BufferedWriter(new OutputStreamWriter(output,"UTF-8"));

    String japanese = "注目のキーワード";   
    InputStream in = new ByteArrayInputStream(japanese.getBytes("UTF-8"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));                  
    String line = null;                 
    while ((line = reader.readLine()) != null) {
        writer.write(line);
        writer.write(System.getProperty(开发者_如何学运维"line.separator"));
    }           
    reader.close(); 
    writer.flush();
    writer.close();
%>

This string in Excel 2007 looks like this : 注目ã®ã‚­ãƒ¼ãƒ¯ãƒ¼ãƒ‰

Could any one please help me here. User should be able to directly open file in Excel when he clicks on download link(above jsp).

Any help would be appreciated.

Thanks Jitendra


I have already answered similar question. Please read here: setting a UTF-8 in java and csv file. Generally the problem can be solved by putting "magic" code (know as Byte Order Mark) in the beginning of the file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜