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

问题描述

我使用此代码通过向csv文件添加波斯语:

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

String[] entries="\u0645 \u062E\u062F\u0627".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();
}

当我打开生成的csv文件时, ứỶờịỆ。

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.

使用无法解决此问题。

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

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

推荐答案

不幸的是,CSV是一种非常特别的格式,没有元数据,将要求灵活编码。

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(如果您正确,它有编码元数据),并让用户导入Excel。

  • 使用来创建实际的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