【发布时间】:2014-10-01 19:06:49
【问题描述】:
我需要阅读我收到的 excel(.xls) 文件。 使用 UTF-8、Cp1252、ISO-8859-1、UTF-16LE 等常规字符集,这些都没有帮助我,字符仍然格式错误。
所以我最终使用juniversalchardet 搜索,它显示字符集是 MacCyrillic,使用 MacCyrillic 读取文件,但仍然是同样奇怪的结果。
当我在 excel 上打开文件时,一切都很好,所有字符都很好,因为它的葡萄牙语充满了 Ç ~ 等。但是打开 whit notepad 或通过 java 文件都搞砸了。 但是,如果在我的 excel 上打开文件,然后像 .txt 一样再次保存它,它就会变得可读
我查找字符集的方法
public static void lerCharset(String fileName) throws IOException {
byte[] buf = new byte[50000000];
FileInputStream fis = new FileInputStream(fileName);
// (1)
UniversalDetector detector = new UniversalDetector(null);
// (2)
int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
// (3)
detector.dataEnd();
// (4)
String encoding = detector.getDetectedCharset();
if (encoding != null) {
System.out.println("Detected encoding = " + encoding);
} else {
System.out.println("No encoding detected.");
}
// (5)
detector.reset();
fis.close();
}
我怎样才能找到正确的字符集? 我应该尝试不同的方法吗?就像让我的java重新保存excel然后开始阅读?
【问题讨论】:
标签: java excel utf-8 character-encoding