String title = "主键,数据源名称,数据源编号,数据源类型,数据库类型,地址,端口,驱动类,用户名,密码,数据库/模式,创建时间,创建人,修改时间,修改人";
String mapKey = "id,dsName,dsCode,dsType,dbType,dsUrl,dsPort,driverClass,dsUserName,dsPassWord,dsSchema,createdTime,createdUser,modiffiedTime,modiffiedUser";
File file = new File("E:\\ioTest\\shujuku.csv");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
boolean b = CsvUtil.doExport(list, title, mapKey, outputStream);
CsvUtil工具类
public class CsvUtil {
private static final String CSV_COLUMN_SEPARATOR = ",";
private static final String CSV_RN = "\r\n";
private static final Logger logger = LoggerFactory.getLogger(CsvUtil.class);
public static boolean doExport(List<Map<String, Object>> dataList, String colNames, String mapKey, OutputStream os) {
try {
StringBuffer buf = new StringBuffer();
String[] colNamesArr = null;
String[] mapKeyArr = null;
colNamesArr = colNames.split(",");
mapKeyArr = mapKey.split(",");
for (String aColNamesArr : colNamesArr) {
buf.append(aColNamesArr).append(CSV_COLUMN_SEPARATOR);
}
buf.append(CSV_RN);
if (null != dataList) {
for (Map<String, Object> aDataList : dataList) {
for (String aMapKeyArr : mapKeyArr) {
buf.append(aDataList.get(aMapKeyArr)).append(CSV_COLUMN_SEPARATOR);
}
buf.append(CSV_RN);
}
}
os.write(buf.toString().getBytes("UTF-8"));
os.flush();
return true;
} catch (Exception e) {
logger.error("doExport错误...", e);
}
return false;
}
}