CSV生成

   public static void createCSV(List                list,
                                   String              filePath,
                                   String              fileName,
                                   String              separator)
                    throws Exception {

        // CSV弌椡弶傔儘僌
        if (log.isInfoEnabled()) {
            log.info(Constants.MSGCI003);
        }

        String sep = DEFAULT_SEPARATOR;

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        // CSV偵彂偔堦峴偺僨乕僞
        String line = "";

        List innerList = new ArrayList();

        // CSV弌椡儕僗僩僨乕僞偑側偄応崌偼張棟懕峴晄壜擻
        if (list == null) {
            return;
        }

        // 嬫愗傝暥帤巜掕偑偁傟偽桪愭偡傞
        if (separator != null && !"".equals(separator)) {
            sep = separator;
        }
       
        try {

            File file = new File(filePath);
           
            // 僷僗偺憂寶偟偰偄側偄応崌
            if (!file.exists()) {

               // 僷僗偺憂寶偡傞
               boolean flag = file.mkdirs();

               if (!flag) {

                   // CSV僼傽僀儖僷僗Exception
                   if (log.isInfoEnabled()) {
                       log.error(Constants.MSGCI007);
                   }
                  
                   return;
               }
            }

            fileName = filePath + "//" + fileName;

            os = new FileOutputStream(fileName, true);
            osw = new OutputStreamWriter(os);
            bw = new BufferedWriter(osw);
           
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getClass() == Object[].class) {
                    line = objectArrToCSVString((Object[]) list.get(i), sep);
                    bw.write(line);
                    bw.newLine();
                } else if (list.get(i).getClass() == ArrayList.class) {
                    innerList = (ArrayList) list.get(i);
                   
                    // csv   
                    line = objectArrToCSVString(innerList.toArray(), sep);
                    //  1峴弌椡
                    bw.write(line);
                    bw.newLine();
                }
            }

            // 僼儔僢僔儏
            bw.flush();
        } finally {
            if (bw != null) {

                // 廔椆張棟
                bw.close();
            }
        }

        // CSV弌椡廔傢傞儘僌
        if (log.isInfoEnabled()) {
            log.info(Constants.MSGCI004);
        }
    }


    private static String objectArrToCSVString(Object[] objArr,
                                               String   sep) {
        String returnStr = "";

        if (objArr != null) {
            for (int i = 0; i < objArr.length; i++) {

                // 嬫愗傝暥帤偺憓擖
                if (0 < i) {
                    returnStr = returnStr + sep;
                }

                // CSV梡偺僨乕僞偵惍宍偟偰捛壛
                returnStr += objectToCSVString(objArr[i]);
            }
        }

        return returnStr;
    }


    private static String objectToCSVString(Object obj) {
        String returnStr = "";

        if (obj != null) {

            returnStr = (String) obj;

            if (returnStr.equals("")) {
                returnStr = "/"/"";
            } else {

                // ["]偼[""]偵曄峏偡傞乮僄僗働乕僾乯
                returnStr = returnStr.replaceAll("/"", "/"/"");
  
                    int length = returnStr.length();
                    if (0 < length) {
                        for (int i = 0; i < length; i++) {
                            char c = returnStr.charAt(i);
                            if (c == ',') {
                            returnStr = "/"" + returnStr + "/"";
                            break;
                        }
                    }
                }
            }
        }

        return returnStr;
    }
}
 

 

随即数字

int randoms = (int)(Math.random() * temp + Integer.parseInt(strDown));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值