String exportPath = "D:"+ File.separatorChar+new SimpleDateFormat("yyyy").format(date)+File.separatorChar
+new SimpleDateFormat("MM").format(date);
//File.separatorChar为了跨平台,根据系统而定是/还是\
File file = new File(exportPath);
if (!file.exists()){//路径不存在则创建
file.mkdirs();
}
File exportFile = new File(exportPath+File.separatorChar
+ "cmClaimSummaryForRecordExport"+".csv");
if (exportFile.exists()) {//文件存在则重命名再创建
exportFile.renameTo(new File(exportPath+File.separatorChar+"cmClaimSummaryForRecordExport"+date.getTime()+".csv"));
exportFile.createTempFile("cmClaimSummaryForRecordExport","csv", new File(exportPath+File.separatorChar));
}else{//否则创建文件写入数据
exportFile.createNewFile();
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(exportFile.getAbsoluteFile()), "UTF-8");
osw.write('\uFEFF');
osw.append(entity.getCsvHeaderEntry());
osw.append(entity.getCsvEntry());
osw.close();
exportFile.setReadable(true, false);//(只读,只有读权限适用于业主的读取权限)
public static String getCsvHeaderEntry() {
return "column1,column2,column3\n";
}
public static String getCsvEntry() {
return "列1的值,列2的值,列3的值\n";
}
//csv文件每行格式是以“,”分隔的,如下图
以文本格式打开
以excel打开