导出数据库信息成csv格式

package com.beagledata.gaea.cebbank.antifraud.aimodel;

/**

  • Created by mahongfei on 2019/3/21.
    */

import com.beagledata.gaea.cebbank.antifraud.entity.FeatureInformation;
import com.beagledata.gaea.cebbank.antifraud.mapper.FeatureInformationMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.;
import java.lang.reflect.Method;
import java.util.
;

/**

  • 文件操作

  • @author qiulinhe

  • @version 2016年12月23日14:21:39
    */
    public class CsvUtil {
    @Autowired
    private static FeatureInformationMapper featureInformationMapper;

    /**

    • 生成为CVS文件

    • @param exportData 源数据List

    • @param fileds

    • @param map csv文件的列表头map

    • @param outPutPath 文件路径

    • @param fileName 文件名称

    • @return
      /
      @SuppressWarnings(“rawtypes”)
      public static File createCSVFile(List exportData, String[] fileds,
      LinkedHashMap map, String outPutPath, String fileName) {
      File csvFile = null;
      BufferedWriter csvFileOutputStream = null;
      try {
      File file = new File(outPutPath);
      if (!file.exists()) {
      file.mkdir();
      }
      // 定义文件名格式并创建
      csvFile = File.createTempFile(fileName, “.csv”,
      new File(outPutPath));
      System.out.println(“csvFile:” + csvFile);
      // UTF-8使正确读取分隔符","
      csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
      new FileOutputStream(csvFile), “GBK”), 1024);
      // 写入文件头部
      for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
      .hasNext(); ) {
      java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
      .next();
      csvFileOutputStream
      .write((String) propertyEntry.getValue() != null ? new String(
      ((String) propertyEntry.getValue())
      .getBytes(“GBK”), “GBK”) : “”);
      if (propertyIterator.hasNext()) {
      csvFileOutputStream.write(",");
      }
      /
      System.out.println(new String(((String) propertyEntry
      .getValue()).getBytes(“GBK”), “GBK”));*/
      }
      csvFileOutputStream.write("\r\n");
      // 写入文件内容,
      for (int j = 0; exportData != null && !exportData.isEmpty()
      && j < exportData.size(); j++) {
      FeatureInformation t = (FeatureInformation) exportData.get(j);
      Class clazz = t.getClass();
      String[] contents = new String[fileds.length];
      for (int i = 0; fileds != null && i < fileds.length; i++) {
      String filedName = toUpperCaseFirstOne(fileds[i]);
      Method method = clazz.getMethod(filedName);
      method.setAccessible(true);
      Object obj = method.invoke(t);
      String str = String.valueOf(obj);
      if (str == null || str.equals(“null”))
      str = “”;
      contents[i] = str;

           }
      
           for (int n = 0; n < contents.length; n++) {
               // 将生成的单元格添加到工作表中
               csvFileOutputStream.write(contents[n]);
               csvFileOutputStream.write(",");
      
           }
           csvFileOutputStream.write("\r\n");
       }
       csvFileOutputStream.flush();
      

      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      try {
      csvFileOutputStream.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      return csvFile;
      }

    /**
    *@Author:mahongfei
    *@description: 删除源文件下的目录及文件
    */
    private static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
    String[] children = dir.list();
    //递归删除目录中的子目录下
    for (int i=0; i<children.length; i++) {
    boolean success = deleteDir(new File(dir, children[i]));
    if (!success) {
    return false;
    }
    }
    }
    // 目录此时为空,可以删除
    return dir.delete();
    }

    /**

    • @Author: mahongfei

    • @description:
      */
      public static void csv(List list) {
      LinkedHashMap map = new LinkedHashMap();
      map.put(“1”, “SysID”);
      map.put(“2”, “ApplyDate”);
      map.put(“3”, “ApproveType”);
      map.put(“4”, “MarkInt”);
      map.put(“5”, “Label”);
      map.put(“6”, “ApplyCardID”);
      map.put(“7”, “ApplyAge”);
      map.put(“8”, “ApplyMobilePhone”);
      map.put(“9”, “ApplyCompanyPhoneZone”);
      map.put(“10”, “ApplyCompanyPhone”);
      map.put(“11”, “ApplyEducation”);
      map.put(“12”, “ApplyIncome”);
      map.put(“13”, “ApplyPosition”);
      map.put(“14”, “ImmediateFamilyRelationCode”);
      map.put(“15”, “ImmediateFamilyMobilePhone”);
      map.put(“16”, “ApplyHomePhone”);
      map.put(“17”, “ApplyJobCode”);
      map.put(“18”, “ImmediateFamilyHomePhone”);
      map.put(“19”, “ImmediateFamilyCompanyPhone”);
      map.put(“20”, “ContactsMobilePhone”);
      map.put(“21”, “ContactsHomePhone”);
      map.put(“22”, “FrequenceForPromoterIn7Days”);
      map.put(“23”, “FrequenceInOneDay”);
      map.put(“24”, “FirstStepDegree”);
      map.put(“25”, “FirstStepFraudDegree”);
      map.put(“26”, “SecondStepDegree”);
      map.put(“27”, “SecondStepFraudDegree”);
      map.put(“28”, “TwoStepNeighBour”);
      map.put(“29”, “TwoStepFraudNeighBour”);
      map.put(“30”, “FirstStepFraudRatio”);
      map.put(“31”, “SecondStepFraudRatio”);
      map.put(“32”, “TwoStepFraudNeighBourRatio”);
      map.put(“33”, “PageRank”);
      map.put(“34”, “ShortestDistance”);
      map.put(“35”, “ClusteringCoefficient”);
      map.put(“36”, “Triangle”);

      String path = “D://export//”;
      String fileName = “特征数据”;
      String fileds[] = new String[]{“sysID”, “applyDate”, “approveType”, “markInt”, “label”, “applyCardID”,
      “applyAge”, “applyMobilePhone”, “applyCompanyPhoneZone”, “applyCompanyPhone”, “applyEducation”,
      “applyIncome”, “applyPosition”, “immediateFamilyRelationCode”, “immediateFamilyMobilePhone”,
      “applyHomePhone”, “applyJobCode”, “immediateFamilyHomePhone”, “immediateFamilyCompanyPhone”,
      “contactsMobilePhone”, “contactsHomePhone”, “frequenceForPromoterIn7Days”, “frequenceInOneDay”,
      “firstStepDegree”, “firstStepFraudDegree”, “secondStepDegree”, “secondStepFraudDegree”,
      “twoStepNeighBour”, “twoStepFraudNeighBour”, “firstStepFraudRatio”, “secondStepFraudRatio”,
      “twoStepFraudNeighBourRatio”, “pageRank”, “shortestDistance”, “clusteringCoefficient”,“triangle”
      };// 设置列英文名(也就是实体类里面对应的列名)
      File file = CsvUtil.createCSVFile(list, fileds, map, path, fileName);
      // String fileName2 = file.getName();
      /boolean success = deleteDir(new File(path));
      if (success) {
      System.out.println("Successfully deleted populated directory: " + path);
      } else {
      System.out.println("Failed to delete populated directory: " + path);
      }
      /
      }

    /**

    • 将第一个字母转换为大写字母并和get拼合成方法
    • @param origin
    • @return
      */
      private static String toUpperCaseFirstOne(String origin) {
      StringBuffer sb = new StringBuffer(origin);
      sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
      sb.insert(0, “get”);
      return sb.toString();
      }

    /* //*

    • 测试数据

    • @param args
      //
      @SuppressWarnings({“rawtypes”, “unchecked”})
      public static void main(String[] args) {
      FeatureInformation featureInformation = new FeatureInformation();
      featureInformation.setSysID(“1”);
      featureInformation.setApplyDate(“1”);
      featureInformation.setApproveType(“1”);
      featureInformation.setMarkInt(“1”);
      featureInformation.setLabel(“1”);
      featureInformation.setApplyCardID(“1”);
      featureInformation.setApplyAge(1.0);
      featureInformation.setApplyMobilePhone(“1”);
      featureInformation.setApplyCompanyPhoneZone(“1”);
      featureInformation.setApplyCompanyPhone(“1”);
      featureInformation.setApplyEducation(“1”);
      featureInformation.setApplyIncome(1.0);
      featureInformation.setApplyPosition(“1”);
      featureInformation.setImmediateFamilyRelationCode(“1”);
      featureInformation.setImmediateFamilyMobilePhone(“1”);
      featureInformation.setApplyHomePhone(“1”);
      featureInformation.setApplyJobCode(“1”);
      featureInformation.setImmediateFamilyHomePhone(“1”);
      featureInformation.setImmediateFamilyCompanyPhone(“1”);
      featureInformation.setContactsMobilePhone(“1”);
      featureInformation.setContactsHomePhone(“1”);
      featureInformation.setFrequenceForPromoterIn7Days(1.0);
      featureInformation.setFrequenceInOneDay(1.0);
      featureInformation.setFirstStepDegree(1.0);
      featureInformation.setFirstStepFraudDegree(1.0);
      featureInformation.setSecondStepDegree(1.0);
      featureInformation.setSecondStepFraudDegree(1.0);
      featureInformation.setTwoStepNeighBour(1.0);
      featureInformation.setTwoStepFraudNeighBour(1.0);
      featureInformation.setFirstStepFraudRatio(1.0);
      featureInformation.setSecondStepFraudRatio(1.0);
      featureInformation.setTwoStepFraudNeighBourRatio(1.0);
      featureInformation.setPageRank(1.0);
      featureInformation.setShortestDistance(1.0);
      featureInformation.setClusteringCoefficient(1.0);
      featureInformation.setTriangle(1.0);
      List list = new LinkedList<>();
      list.add(featureInformation);

      LinkedHashMap map = new LinkedHashMap();
      map.put(“1”, “SysID”);
      map.put(“2”, “ApplyDate”);
      map.put(“3”, “ApproveType”);
      map.put(“4”, “MarkInt”);
      map.put(“5”, “Label”);
      map.put(“6”, “ApplyCardID”);
      map.put(“7”, “ApplyAge”);
      map.put(“8”, “ApplyMobilePhone”);
      map.put(“9”, “ApplyCompanyPhoneZone”);
      map.put(“10”, “ApplyCompanyPhone”);
      map.put(“11”, “ApplyEducation”);
      map.put(“12”, “ApplyIncome”);
      map.put(“13”, “ApplyPosition”);
      map.put(“14”, “ImmediateFamilyRelationCode”);
      map.put(“15”, “ImmediateFamilyMobilePhone”);
      map.put(“16”, “ApplyHomePhone”);
      map.put(“17”, “ApplyJobCode”);
      map.put(“18”, “ImmediateFamilyHomePhone”);
      map.put(“19”, “ImmediateFamilyCompanyPhone”);
      map.put(“20”, “ContactsMobilePhone”);
      map.put(“21”, “ContactsHomePhone”);
      map.put(“22”, “FrequenceForPromoterIn7Days”);
      map.put(“23”, “FrequenceInOneDay”);
      map.put(“24”, “FirstStepDegree”);
      map.put(“25”, “FirstStepFraudDegree”);
      map.put(“26”, “SecondStepDegree”);
      map.put(“27”, “SecondStepFraudDegree”);
      map.put(“28”, “TwoStepNeighBour”);
      map.put(“29”, “TwoStepFraudNeighBour”);
      map.put(“30”, “FirstStepFraudRatio”);
      map.put(“31”, “SecondStepFraudRatio”);
      map.put(“32”, “TwoStepFraudNeighBourRatio”);
      map.put(“33”, “PageRank”);
      map.put(“34”, “ShortestDistance”);
      map.put(“35”, “ClusteringCoefficient”);
      map.put(“36”, “Triangle”);

      String path = “D://export//”;
      String fileName = “特征数据”;
      String fileds[] = new String[]{“sysID”, “applyDate”, “approveType”, “markInt”, “label”, “applyCardID”,
      “applyAge”, “applyMobilePhone”, “applyCompanyPhoneZone”, “applyCompanyPhone”, “applyEducation”,
      “applyIncome”, “applyPosition”, “immediateFamilyRelationCode”, “immediateFamilyMobilePhone”,
      “applyHomePhone”, “applyJobCode”, “immediateFamilyHomePhone”, “immediateFamilyCompanyPhone”,
      “contactsMobilePhone”, “contactsHomePhone”, “frequenceForPromoterIn7Days”, “frequenceInOneDay”,
      “firstStepDegree”, “firstStepFraudDegree”, “secondStepDegree”, “secondStepFraudDegree”,
      “twoStepNeighBour”, “twoStepFraudNeighBour”, “firstStepFraudRatio”, “secondStepFraudRatio”,
      “twoStepFraudNeighBourRatio”, “pageRank”, “shortestDistance”, “clusteringCoefficient”,“triangle”
      };// 设置列英文名(也就是实体类里面对应的列名)
      File file = CsvUtil.createCSVFile(list, fileds, map, path, fileName);
      String fileName2 = file.getName();
      System.out.println(“文件名称:” + fileName2);
      //boolean success = deleteDir(new File(path));
      if (success) {
      System.out.println("Successfully deleted populated directory: " + path);
      } else {
      System.out.println("Failed to delete populated directory: " + path);
      }
      //

      }*/
      }

猜你喜欢

转载自blog.csdn.net/weixin_43812065/article/details/89848748