包含日语字符的 CSV 文件无法在 Excel 2007 中正确打开

发布于 2024-10-05 07:56:41 字数 1418 浏览 1 评论 0原文

我有一个简单的JSP来下载包含日语字符的csv文件。下载的文件在 notepad++ 中正确打开并显示正确的字符,但是当我尝试在 Excel 2007 中打开同一文件时,它显示一些不同的字符,可能是其他编码。

我的 JSP 看起来像这样。

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

Excel 2007 中的这个字符串看起来像这样: 泡目ã®ã‚¤¡¡¡¡¡¡¡¡¡¡

任何人都可以在这里帮助我。当用户单击下载链接(jsp 上方)时,应该能够直接在 Excel 中打开文件。

任何帮助将不胜感激。

谢谢 吉滕德拉

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-10-12 07:56:41

我已经回答过类似的问题了。请阅读此处:设置 UTF-8 java 和 csv 文件。一般来说,可以通过在文件开头放置“神奇”代码(称为字节顺序标记)来解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文