如何在Java中将数据从Mongo DB导出为Excel格式

时间:2018-08-08 05:46:08

标签: java mongodb

MongoDB中的架构: {
    “名称”:“ Kamala”,     “流量”:[

    {
        "Date" : "2018-08-03",
        "New" : "set",
        "Form" : {
            "y" : "1",
            "d" : "3",
            "p" : "3"
        }
    }
]

}

我的Java代码: 公共静态void main(String [] args){

     String db = "Database";
     String col = "col";
     String Host="localhost";
     String Port="27017";
     String fileName = "D:/File/sample.xls";

     String Query = "C:\\Program Files\\MongoDB\\Server\\3.4\\bin\\mongoexport.exe --host " + Host + " --port " + Port + " --db " + db + " --collection " + col + " --type=xls--fields Name,Flows, --out " + fileName + "";

     try {
         System.out.println(Query);

         StringTokenizer st = new StringTokenizer(Query);
         String[] Queryarray = new String[st.countTokens()];
         for (int i = 0; st.hasMoreTokens(); i++)
             Queryarray[i] = st.nextToken();

         ProcessBuilder processBuilder = new ProcessBuilder(Queryarray);
         processBuilder.redirectErrorStream(true);

         Process process = processBuilder.start();
         BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));

         String data = "";
         while ((data = processOutput.readLine()) != null) {
             System.out.println(data);
         }


     } catch (Exception e) {
         e.printStackTrace();
     }
 }

我正在尝试检索Flows数组中的日期,新值,表单值

0 个答案:

没有答案