摘要:java + CSV
String filenm = "元件匯出表.csv";
FileOutputStream fos = new FileOutputStream(filenm);
//Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filenm), "UTF8"));
//新增標題列
int contI = 0;
List<Ydd101wDto> yddPropData = yddPropertyService.findPropertyByTypeid(typeId);
for(Ydd101wDto data: yddPropData) {
contI ++;
if (contI % yddPropData.size() == 0) { //加上分欄
fos.write(data.getResourcePropertyName().getBytes());
} else {
fos.write((data.getResourcePropertyName() + ",").getBytes());
}
}
//換行
fos.write("\n".getBytes());
//新增內容
contI = 0;
List<Map<String,Object>> yddDec = yddDescriptionService.findDescripByTypeid(typeId);
for (Map<String, Object> key : yddDec) {
contI ++;
for (Ydd101wDto propData: yddPropData) {
String value = key.get("RESOURCE_DESCRIPTION_VALUE") == null ? "":key.get("RESOURCE_DESCRIPTION_VALUE").toString();
if (key.get("RESOURCE_TYPE_PROPERTY").toString().toLowerCase().contains(propData.getResourcePropertyName().toLowerCase())) {
if (contI % yddPropData.size() == 0) { //加上分欄
fos.write(new String(value.getBytes("UTF8")).getBytes());
} else {
fos.write((new String(value.getBytes("UTF8")) + ",").getBytes());
}
}
}
if (contI % yddPropData.size() == 0) { //換行
fos.write("\n".getBytes());
}
}
//out.close();
fos.flush();
fos.close();