Java CSV 导出 在日文环境下乱码解决

public  void createCsvFile(String fileName,String sep, Object titalObj, List dataList) throws  IOException {
response.reset();
response.setCharacterEncoding("UTF-8");
String sfilename = URLDecoder.decode(fileName, "UTF-8"); 
response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".csv");

// 设置CSV分隔符号
sep = sep == null ? "," : (sep.equals("2") ? "\t" : ",");
OutputStream os = null;
os = response.getOutputStream();
os.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }); //此处为解决乱码的必要代码 并且在下面用到getBytes()方法的地方,写作getBytes("UTF-8")
List titalList = null;
String[] titalArray = null;
if(titalObj instanceof List){ //判断标题参数的类型,如果是List
titalList = (List)titalObj;
if(titalList!=null&&titalList.size()>0){
Object obj = null;
List testList = null;
String testStr = "";
for(int i=0;i obj = titalList.get(i);
if(obj instanceof List){
testList = (List)obj;
for(int j=0;j if(j>0){
os.write(sep.getBytes("UTF-8"));
}
testStr = ActyCommonUtil.killNull(testList.get(j));
if (testStr!=null&&sep.equals(",")&&testStr.contains(",")){
testStr="\""+testStr+"\"";
};
os.write(testStr.getBytes("UTF-8"));
}
os.write(System.getProperty("line.separator").getBytes("UTF-8"));
}else if(obj!=null&&obj instanceof String){
if(i>0){
os.write(sep.getBytes("UTF-8"));
}
testStr = ActyCommonUtil.killNull(String.valueOf(obj));
if (testStr!=null&&sep.equals(",")&&testStr.contains(",")){
testStr="\""+testStr+"\"";
};
os.write(testStr.getBytes("UTF-8"));
}

}
if(obj instanceof String){
os.write(System.getProperty("line.separator").getBytes("UTF-8"));
}
}
}else if(titalObj instanceof String[]){
titalArray = (String[])titalObj;
if(titalArray!=null&&titalArray.length>0){
for(int i=0;i if(i>0){
os.write(sep.getBytes("UTF-8"));
}
os.write(ActyCommonUtil.killNull(titalArray[i]).getBytes("UTF-8"));
}
os.write(System.getProperty("line.separator").getBytes("UTF-8"));
}
}

Object objData = null;
if(dataList!=null&&dataList.size()>0){
objData = dataList.get(0);
if(objData instanceof List){
List> data = (List>)dataList;
for (List row : data) {
int i=0,j=row.size();
for (String col : row) {
if (col!=null&&sep.equals(",")&&col.contains(",")){
col="\""+col+"\"";
};
os.write(col.getBytes("UTF-8"));
i++;
if (i os.write(sep.getBytes("UTF-8"));
}
}
os.write(System.getProperty("line.separator")
.getBytes("UTF-8"));
}
os.flush();
}
}
os.close();
}

你可能感兴趣的:(Java CSV 导出 在日文环境下乱码解决)