最近又遇到了需要提供csv下载功能的需求,不同的时需要用Java来实现,心想简单,就把以前PHP的版本重写了一遍,然后生成一份csv,用excel2007打开一看,里面的中文都是乱码,一下就懵了,以前好好的功能怎么突然不行了??以前也一直用2007的啊!于是开始了漫长的google之旅。
看来看去,说的都是输出utf-8格式的csv需要在文件头先输出BOM(BOM不懂的可以google了),即0xEF 0xBB 0xBF三个字节,这样更摸不着头脑了,明明是对的,偏偏不成功,直到发现一个帖子:http://*.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文件:
[java] view plain copy
- import java.io.*;
-
-
-
-
- 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);
- os.write(187);
- os.write(191);
-
- PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
-
- w.print(line);
- w.flush();
- w.close();
- }
- }