Java - 问题集 - 导出csv文件中文乱码

时间:2021-07-10 08:02:41

微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头。

FileOutputStream fos = new FileOutputStream(new File("d:\\test.csv"));
// 写入bom头
byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
fos.write(uft8bom); Writer writer = new BufferedWriter(
new OutputStreamWriter(
fos, "UTF-8"));
CsvExporter csvExporter = new CsvExporter(writer);
csvExporter.addColumn(new Column("company","公司"));
csvExporter.addColumn(new Column("email","邮箱"));
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map = new HashMap<String,String>();
map.put("company", "公司1");
map.put("email", "邮箱1");
list.add(map);
csvExporter.export(list);;
csvExporter.close();