将数据写入到CSV文件


  1. /*** 
  2.      * 生成csv文件 
  3.      *  
  4.      * @param context 
  5.      * @param list 
  6.      *            对象集合 
  7.      * @param csvName 
  8.      *            对应的文件名称 
  9.      * @param propertyArray 
  10.      *            属性字符串,逗号分割 
  11.      */  
  12.     public static void writeCsvFile(Context context, List list, String csvName,  
  13.             String folder, String propertyArray) {  
  14.         if (list == null || list.isEmpty())  
  15.             return;  
  16.   
  17.         // sdcard/com/cela/目录下  
  18.         String path = DataUtils.getCsvFilePath(folder);  
  19.         File file = new File(path);  
  20.         if (!file.exists()) {  
  21.             file.mkdirs();  
  22.         }  
  23.         path += csvName;  
  24.   
  25.         // 判断文件是否存在,如果存在则取出原有数据  
  26.         List<String[]> originalList = null;  
  27.         try {  
  28.             File f = new File(path);  
  29.             if (f.exists()) {  
  30.                 originalList = new ArrayList<String[]>();  
  31.                 CSVReader reader = new CSVReader(new FileReader(path), ',');  
  32.                 originalList.addAll(reader.readAll());  
  33.             }  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.   
  38.         OutputStreamWriter fw = null;  
  39.         CSVWriter csv = null;  
  40.         // 通过反射获取list里面的值  
  41.         try {  
  42.             fw = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");  
  43.             csv = new CSVWriter(fw, ',');  
  44.             final int size = list.size();  
  45.             // 写入原有数据  
  46.             if (originalList != null && !originalList.isEmpty()) {  
  47.                 final int len = originalList.size();  
  48.                 for (int i = 0; i < len; i++) {  
  49.                     csv.writeNext(originalList.get(i));  
  50.                 }  
  51.             }  
  52.             // 开始写入对象  
  53.             for (int i = 0; i < size; i++) {  
  54.                 Object obj = list.get(i);  
  55.                 Class cla = obj.getClass();  
  56.                 String[] propertys = propertyArray.split(",");  
  57.                 String[] values = new String[propertys.length];  
  58.                 for (int j = 0; j < propertys.length; j++) {  
  59.                     Field field = cla.getDeclaredField(propertys[j]);  
  60.                     String fname = field.getName();  
  61.                     String strLitter = fname.substring(01).toUpperCase();  
  62.   
  63.                     String getName = "get" + strLitter + fname.substring(1);  
  64.                     String setName = "set" + strLitter + fname.substring(1);  
  65.   
  66.                     Method getmethod = cla.getMethod(getName, new Class[] {});  
  67.                     cla.getMethod(setName, new Class[] { field.getType() });  
  68.   
  69.                     Object value = getmethod.invoke(obj, new Object[] {});  
  70.                     if(value!=null){  
  71.                         values[j] = (String) value.toString();  
  72.                     }else{  
  73.                         values[j] = "";  
  74.                     }  
  75.                 }  
  76.                 csv.writeNext(values);  
  77.             }  
  78.         } catch (Exception e) {  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             if (csv != null) {  
  82.                 try {  
  83.                     csv.flush();  
  84.                     csv.close();  
  85.                 } catch (IOException e) {  
  86.                     e.printStackTrace();  
  87.                 }  
  88.             }  
  89.         }  
  90.     }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值