本文介绍了在 java 和 csv 文件中设置 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码通过 OpenCSV 将波斯语单词添加到 csv 文件:

I am using this code for add Persian words to a csv file via OpenCSV:

String[] entries="u0645 u062Eu062Fu0627".split("#");
try{
    CSVWriter writer=new CSVWriter(new OutputStreamWriter(new FileOutputStream("C:\test.csv"), "UTF-8"));

    writer.writeNext(entries);
    writer.close();
}
catch(IOException ioe){
    ioe.printStackTrace();
}

当我在 Excel 中打开生成的 csv 文件时,它包含 "ứỶờịỆ".其他程序如 notepad.exe 没有这个问题,但我所有的用户都使用 MS Excel.

When I open the resulting csv file, in Excel, it contains "ứỶờịỆ". Other programs such as notepad.exe don't have this problem, but all of my users are using MS Excel.

SuperCSV 替换 OpenCSV 并不能解决这个问题.

Replacing OpenCSV with SuperCSV does not solve this problem.

当我手动将波斯字符输入到 csv 文件中时,我没有任何问题.

When I typed Persian characters into csv file manually, I don't have any problems.

推荐答案

不幸的是,CSV 是一种非常临时的格式,没有元数据,也没有要求灵活编码的真正标准.只要您使用 CSV,就不能可靠地使用 ASCII 之外的任何字符.

Unfortunately, CSV is a very ad hoc format with no metadata and no real standard that would mandate a flexible encoding. As long as you use CSV, you can't reliably use any characters outside of ASCII.

您的选择:

  • 写入 XML(如果操作正确,它确实有编码元数据)并让用户 将 XML 导入 Excel.
  • 使用 Apache POI 创建实际的 Excel 文档.
  • Write to XML (which does have encoding metadata if you do it right) and have the users import the XML into Excel.
  • Use Apache POI to create actual Excel documents.

这篇关于在 java 和 csv 文件中设置 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:38