龙空技术网

java 创建csv通用接口;适合各种类

西瓜爱好玩家 294

前言:

而今兄弟们对“csvjava”大体比较讲究,看官们都想要剖析一些“csvjava”的相关资讯。那么小编也在网摘上网罗了一些对于“csvjava””的相关知识,希望咱们能喜欢,咱们一起来学习一下吧!

Object o = datas.get(0);

String objName = o.getClass().getSimpleName();

DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

String csvFilePath = filePath + objName + dateFormat.format(new Date()) + ".csv";

File file = new File(csvFilePath);

if (!file.exists() && !file.isDirectory()) {

file.mkdirs();

}

//构造头部数据

String[] title = ObjectTransferUtils.generateObjAttr(o);

CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(title);

OutputStreamWriter ouputStream = new OutputStreamWriter(out, "GBK");

CSVPrinter printer = new CSVPrinter(ouputStream, csvFormat);

for (int i = 0; i < datas.size(); i++) {

Object obj = datas.get(i);

String[] titl = ObjectTransferUtils.generateObjAttr(obj);

Object[] value = new Object[titl.length];

for (int t = 0; t < titl.length; t++) {

if (titl[t] == null) {

continue;

}

value[t] = ObjectTransferUtils.generateAttrValue(titl[t], obj);

}

printer.printRecord(value);

}

标签: #csvjava