- 使用数组定义【表头】信息
private final String[] cpntTitles = new String[] {"服务组件ID","服务组件名称","组件描述","状态","关联的服务组件配置ID","组件类型"};
- 使用数组定义【JavaBean】,对象属性名称和【表头】定义的一一对应
private final String[] cpntPropertys = new String[] {"reqmentId","cpntNm","funcDescr","stusCd","confId","cpntTyp"};
- 创建 javaBean
public class ComponentBean {
private String reqmentId;
private String cpntNm;
private String cpntTyp;
private String funcDescr;
private String stusCd;
private String domainId;
private String confId;
public String getReqmentId() {
return reqmentId;
}
public void setReqmentId(String reqmentId) {
this.reqmentId = reqmentId;
}
public String getCpntNm() {
return cpntNm;
}
public void setCpntNm(String cpntNm) {
this.cpntNm = cpntNm;
}
public String getCpntTyp() {
return cpntTyp;
}
public void setCpntTyp(String cpntTyp) {
this.cpntTyp = cpntTyp;
}
public String getFuncDescr() {
return funcDescr;
}
public void setFuncDescr(String funcDescr) {
this.funcDescr = funcDescr;
}
public String getStusCd() {
return stusCd;
}
public void setStusCd(String stusCd) {
this.stusCd = stusCd;
}
public String getDomainId() {
return domainId;
}
public void setDomainId(String domainId) {
this.domainId = domainId;
}
public String getConfId() {
return confId;
}
public void setConfId(String confId) {
this.confId = confId;
}
}
- 创建导出数据格式,List list
private void createCpntList(int index, BusinessLogicDefinition businessLogicDefinition, ExportBean exportBean,
List<ComponentBean> cpntList) {
ComponentBean componentBean = new ComponentBean();
componentBean.setReqmentId("123");
componentBean.setCpntNm("xxx");
componentBean.setCpntTyp("\t01");
componentBean.setDomainId("11");
componentBean.setFuncDescr("xxx");
componentBean.setStusCd("\t03");
componentBean.setConfId("123");
cpntList.add(componentBean);
}
- 生成csv文件
public <T> void exportCsv(String filePath,String[] titles,String[] propertys,List<T> list) throws Exception{
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (filePath,true),charSet));
try {
for(String title : titles){
writer.write(title);
writer.write(",");
}
writer.write("\r\n");
for(Object obj : list){
Field[] fields = obj.getClass().getDeclaredFields();
for(String property : propertys){
for(Field field : fields){
field.setAccessible(true);
if(property.equals(field.getName())){
writer.write(String.valueOf(field.get(obj)));
writer.write(",");
continue;
}
}
}
writer.write("\r\n");
}
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
writer.close();
}
}