您的位置 首页 java

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

Object o = datas.get(0);

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

DateFormat dateFormat = new SimpleDateFormat(“yyyyMMddHHmmss”);

String csvFilePath = file Path + 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);

}

文章来源:智云一二三科技

文章标题:java 创建csv通用接口;适合各种类

文章地址:https://www.zhihuclub.com/181499.shtml

关于作者: 智云科技

热门文章

网站地图