码迷,mamicode.com
首页 > 其他好文 > 详细

避免UTF-8的csv文件打开中文出现乱码

时间:2015-05-29 23:11:24      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:excel   utf-8   中文   乱码   utf-16le   

最近又遇到了需要提供csv下载功能的需求,不同的时需要用java来实现,心想简单,就把以前php的版本重写了一遍,然后生成一份csv,用excel2007打开一看,里面的中文都是乱码,一下就懵了,以前好好的功能怎么突然不行了??以前也一直用2007的啊!于是开始了漫长的google之旅。

看来看去,说的都是输出utf-8格式的csv需要在文件头先输出BOM(BOM不懂的可以google了),即0xEF 0xBB 0xBF三个字节,这样更摸不着头脑了,明明是对的,偏偏不成功,直到发现一个帖子:http://stackoverflow.com/a/9337150/1794493 ,里面提到2007需要装sp3才能识别BOM,shit!原来是这回事!里面同时又提到,用utf-16le编码输出貌似更通用,经测试确实如此,但是utf-16le的BOM是0xFF 0xFE,帖子里面说错了!下面是一个简单的测试结果:

excel版本 附加包 编码  测试结果
2007 sp3 utf-8 yes
2007 utf-8 no
2007 sp3 utf-16le yes
2007 utf-16le yes
2011 utf-8 no
2011 utf-16le yes

因为条件有限,只测试了这几个版本,可见utf-16le是更通用的编码格式。下面附上java代码,main方法中采用utf-16le编码,最后调用了utf8编码的方法,最后会输出两种编码格式的csv文件:

import java.io.*;

/**
 * Created by zhaozhi on 15-5-29.
 */
public class TestCSV {

    public static String join(String[] strArr, String delim) {
        StringBuilder sb = new StringBuilder();
        for(String s : strArr) {
            sb.append(s);
            sb.append(delim);
        }
        String ret;
        if (strArr.length > 1) {
            ret = sb.substring(0, sb.length()-1);
        }
        else {
            ret = sb.toString();
        }
        return ret;
    }
    public static  void main (String[] args) throws  Exception {
        String[] heads = {"日期", "产品", "订单数"};
        String[][] rows = {
                {"20150228", "安卓", "23"},
                {"20150301", "web", "34"}
        };
        byte[] bom = {(byte)0xFF, (byte)0xFE};
        String fname = "d:\\utf-16le.csv";
        BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(fname));
        bo.write(bom);
        bo.write(join(heads, "\t").getBytes("utf-16le"));
        bo.write("\n".getBytes("utf-16le"));
        for (String[] row : rows) {
            bo.write(join(row, "\t").getBytes("utf-16le"));
            bo.write("\n".getBytes("utf-16le"));
        }
        bo.close();

        UTF8();
    }

    public static void UTF8() throws IOException {
        String line = "中文,标题,23";
        OutputStream os = new FileOutputStream("d:/utf-8.csv");
        os.write(239);   // 0xEF
        os.write(187);   // 0xBB
        os.write(191);   // 0xBF

        PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

        w.print(line);
        w.flush();
        w.close();
    }
}


避免UTF-8的csv文件打开中文出现乱码

标签:excel   utf-8   中文   乱码   utf-16le   

原文地址:http://blog.csdn.net/zhaozhi406/article/details/46241415

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!