java 导出数据生成csv文件

本文介绍了一种使用Java实现CSV文件导出的方法。通过定义表头信息和服务组件JavaBean,创建了包含具体数据的服务组件列表,并最终生成了CSV格式的文件。文章详细展示了如何通过反射机制来遍历对象属性并将其写入CSV文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 使用数组定义【表头】信息
private final String[] cpntTitles = new String[] {"服务组件ID","服务组件名称","组件描述","状态","关联的服务组件配置ID","组件类型"};
  1. 使用数组定义【JavaBean】,对象属性名称和【表头】定义的一一对应
	private final String[] cpntPropertys = new String[] {"reqmentId","cpntNm","funcDescr","stusCd","confId","cpntTyp"};

  1. 创建 javaBean
public class ComponentBean {

    /**
     * 服务组件ID
     */
    private String reqmentId;

    /**
     * 服务组件名称
     */
    private String cpntNm;

    /**
     * 组件类型�?01:服务组件;02:页�?+交互组件
     */
    private String cpntTyp;

    /**
     * 组件描述
     */
    private String funcDescr;

    /**
     * 状态:00-待开发;02-开发中;03开发完成
     */
    private String stusCd;

    /**
     * 所属领域
     */
    private String domainId;

    /**
     *关联的服务组件配置ID
     */
    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;
	}
    
    
    
}
  1. 创建导出数据格式,List list
	/**
	 * 创建服务组件信息集合
	 * @param index
	 * @param businessLogicDefinition
	 * @param exportBean
	 * @param cpntList
	 */
	 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);
		
	}
  1. 生成csv文件
 /**
     *  导出生成csv格式的文件
     * @param filePath 存储文件路径
     * @param   titles csv格式头文
     * @param   propertys 需要导出的数据实体的属性,注意与title一一对应
     * @param   list 需要导出的对象集合
     * @return
     * @throws Exception 
     */
    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 {
		     //csv文件是逗号分隔,除第一个外,每次写入一个单元格数据后需要输入逗号
		     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(",");
//                        System.out.println(field.get(obj));
                        continue;
			        }
			       }
			      }
		      //写完一行换行
		     writer.write("\r\n");
		     }
		     writer.flush();
	     } catch (Exception e) {
	    	e.printStackTrace();
//			throw new Exception("生成【"+filePath+"】失败");
	     }finally {
	        	writer.close();
	     }
	     
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值