1. 程式人生 > >Code Backup : create CSV file

Code Backup : create CSV file

/**
* 方法名稱:生成CSV檔案

* 概要說明:

*
* @param results 結果集
* @param errors 錯誤結果集
* @param outPutPath 輸出檔案路徑
* @param fileName 輸出檔名稱
*
* @return File CSV檔案
*
*/
public File createCSVFile(List<Map<String, String>> results, List<Map<String, String>> errors, String outPutPath,
String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdirs();
}
csvFile = File.createTempFile(fileName, “.csv”, new File(outPutPath));
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), “UTF-8”),
1024);
csvFileOutputStream.write(“標識,編號,名稱,路線編號,樁號,經度,緯度,起點樁號,起點經度,起點緯度,終點樁號,終點經度,終點緯度,行政區劃程式碼,模糊匹配線路編號,備註”);
for (Map<String, String> result : results) {
csvFileOutputStream.newLine();
csvFileOutputStream.write(result.get(“BS”) == null ? “,” : result.get(“BS”).toString() + “,”);
csvFileOutputStream.write(result.get(“BH”) == null ? “,” : result.get(“BH”).toString() + “,”);
csvFileOutputStream.write(result.get(“MC”) == null ? “,” : result.get(“MC”).toString() + “,”);
csvFileOutputStream.write(result.get(“LXBH”) == null ? “,” : result.get(“LXBH”).toString() + “,”);
csvFileOutputStream.write(result.get(“GCZZH”) == null ? “,” : result.get(“GCZZH”).toString() + “,”);
csvFileOutputStream.write(result.get(“GCZJD”) == null ? “,” : result.get(“GCZJD”).toString() + “,”);
csvFileOutputStream.write(result.get(“GCZWD”) == null ? “,” : result.get(“GCZWD”).toString() + “,”);
csvFileOutputStream.write(result.get(“QDZH”) == null ? “,” : result.get(“QDZH”).toString() + “,”);
csvFileOutputStream.write(result.get(“QDJD”) == null ? “,” : result.get(“QDJD”).toString() + “,”);
csvFileOutputStream.write(result.get(“QDWD”) == null ? “,” : result.get(“QDWD”).toString() + “,”);
csvFileOutputStream.write(result.get(“ZDZH”) == null ? “,” : result.get(“ZDZH”).toString() + “,”);
csvFileOutputStream.write(result.get(“ZDJD”) == null ? “,” : result.get(“ZDJD”).toString() + “,”);
csvFileOutputStream.write(result.get(“ZDWD”) == null ? “,” : result.get(“ZDWD”).toString() + “,”);
csvFileOutputStream.write(result.get(“XZQH”) == null ? “,” : result.get(“XZQH”).toString() + “,”);
csvFileOutputStream
.write(result.get(“fuzzyLXBH”) == null ? “,” : result.get(“fuzzyLXBH”).toString() + “,”);
csvFileOutputStream.write(result.get(“REASON”) == null ? “” : result.get(“REASON”).toString() + “”);
}
csvFileOutputStream.newLine();
csvFileOutputStream.write(“errors”);
for (Map<String, String> error : errors) {
csvFileOutputStream.newLine();
csvFileOutputStream.write(error.get(“BS”) == null ? “,” : error.get(“BS”).toString() + “,”);
csvFileOutputStream.write(error.get(“BH”) == null ? “,” : error.get(“BH”).toString() + “,”);
csvFileOutputStream.write(error.get(“MC”) == null ? “,” : error.get(“MC”).toString() + “,”);
csvFileOutputStream.write(error.get(“LXBH”) == null ? “,” : error.get(“LXBH”).toString() + “,”);
csvFileOutputStream.write(error.get(“GCZZH”) == null ? “,” : error.get(“GCZZH”).toString() + “,”);
csvFileOutputStream.write(error.get(“GCZJD”) == null ? “,” : error.get(“GCZJD”).toString() + “,”);
csvFileOutputStream.write(error.get(“GCZWD”) == null ? “,” : error.get(“GCZWD”).toString() + “,”);
csvFileOutputStream.write(error.get(“QDZH”) == null ? “,” : error.get(“QDZH”).toString() + “,”);
csvFileOutputStream.write(error.get(“QDJD”) == null ? “,” : error.get(“QDJD”).toString() + “,”);
csvFileOutputStream.write(error.get(“QDWD”) == null ? “,” : error.get(“QDWD”).toString() + “,”);
csvFileOutputStream.write(error.get(“ZDZH”) == null ? “,” : error.get(“ZDZH”).toString() + “,”);
csvFileOutputStream.write(error.get(“ZDJD”) == null ? “,” : error.get(“ZDJD”).toString() + “,”);
csvFileOutputStream.write(error.get(“ZDWD”) == null ? “,” : error.get(“ZDWD”).toString() + “,”);
csvFileOutputStream.write(error.get(“XZQH”) == null ? “,” : error.get(“XZQH”).toString() + “,”);
csvFileOutputStream
.write(error.get(“fuzzyLXBH”) == null ? “,” : error.get(“fuzzyLXBH”).toString() + “,”);
csvFileOutputStream.write(error.get(“REASON”) == null ? “” : error.get(“REASON”).toString() + “”);
}
csvFileOutputStream.close();
} catch (Exception e) {
return null;
}
return csvFile;
}

/**
 * <B>方法名稱:</B>生成CSV檔案<BR>
 * <B>概要說明:</B><BR>
 * 
 * @param results 結果集
 * @param errors 錯誤結果集
 * @param outPutPath 輸出檔案路徑
 * @param fileName 輸出檔名稱
 * 
 * @return File CSV檔案
 * 
 */
public File createDDLFile(List<Map<String, String>> results, List<Map<String, String>> errors, String outPutPath,
        String fileName) {
    File file = new File(outPutPath + fileName + ".txt");
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        PrintWriter printWriter = new PrintWriter(fileOutputStream);
        StringBuffer sql = new StringBuffer();
        for (int i = 0; i < results.size(); i++) {
            try {
                if (!results.get(i).containsKey("QDJD") || !results.get(i).containsKey("QDWD")
                        || !results.get(i).containsKey("ZDJD") || !results.get(i).containsKey("ZDWD")) {
                    continue;
                }
                sql.setLength(0);
                sql.append(" UPDATE STAKE_INFO SET ");
                sql.append(" SSTARTLNG = "
                        + (!results.get(i).containsKey("QDJD") ? "null" : results.get(i).get("QDJD").toString())
                        + ",");
                sql.append(" SSTARTLAT = "
                        + (!results.get(i).containsKey("QDWD") ? "null" : results.get(i).get("QDWD").toString())
                        + ",");
                sql.append(" SENDLNG = "
                        + (!results.get(i).containsKey("ZDJD") ? "null" : results.get(i).get("ZDJD").toString())
                        + ",");
                sql.append(" SENDLAT = "
                        + (!results.get(i).containsKey("ZDWD") ? "null" : results.get(i).get("ZDWD").toString())
                        + " ");
                sql.append(" WHERE GCZBS = '" + results.get(i).get("BS").toString() + "' ");
                sql.append(" AND SID = '" + results.get(i).get("BH").toString() + "' ");
                sql.append(" AND SNAME = '" + results.get(i).get("MC").toString() + "' ");
                sql.append(" AND SROADNUMBER = '" + results.get(i).get("LXBH").toString() + "';\n");
                printWriter.write(sql.toString());
                printWriter.flush();
            } catch (Exception e) {
                printWriter.write("//第" + i + "條資料異常,請檢查!\n");
                continue;
            }
        }
        printWriter.close();
    } catch (Exception e) {
        return null;
    }
    return file;
}