解决Excel打开UTF-8编码的CSV乱码的问题

之前遇到了使用UTF-8编码的CSV文件,使用Excel打开乱码的问题,在经过一番Google之后,
发现可以通过在csv的文件头设置BOM(Byte order mark)来解决。

The byte order mark (BOM) is a Unicode character, U+FEFF Byte order mark (BOM), whose appearance as a magic number at the start of a text stream can signal several things to a program consuming the text

关于BOM具体的解释,感兴趣的可以阅读本文参考资料中给出的维基百科的链接。
如果读者想尽快的知道如何在程序中解决,可以阅读下面的代码。

解决代码

  private final Joiner joiner = Joiner.on(",");
 interface LineProcessor<T> {
        List<String> processor(T object);
 }
   private <T> void writeToCsv(String fileName, String[] header, List<T> body,
                                LineProcessor<T> lineProcessor) {
        Closer closer = Closer.create();
        try {
            File file = new File(fileName);
            if (file.exists()) {
                if (!file.delete()) {
                    logger.warn("文件:{}删除失败");
                }
            }
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file), Charsets.UTF_8));
            BufferedWriter bufferedWriter = closer.register(out);
            out.write('\ufeff');
            bufferedWriter.write(joiner.join(header));
            bufferedWriter.newLine();
            for (T o : body) {
                List<String> strings = lineProcessor.processor(o);
                bufferedWriter.write(joiner.join(strings));
                bufferedWriter.newLine();
            }
        } catch (Throwable e) {
            try {
                throw closer.rethrow(e);
            } catch (IOException e1) {
                logger.error(e1.getMessage(), e1);
                throw Throwables.propagate(e1);
            }
        } finally {
            try {
                closer.close();
            } catch (IOException e) {
                logger.error("csv文件流关闭失败:{}", e.getMessage(), e);
            }
        }
    }

上面代码的核心就是:out.write('\ufeff');,通过写入BOM:\ufeff来解决

参考资料

本文版权归作者所有,禁止一切形式的转载,复制等操作
赞赏

微信赞赏支付宝赞赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注