想要使用硒中的Java将从mongodb集合接收的数据保存到excel

时间:2018-11-28 13:23:16

标签: java excel mongodb selenium-webdriver

我正在使用带有日食的鸦片。在我的项目中,我需要从Mongo DB中读取我的帐户的配置数据,并根据分配给密钥的值执行代码。我的配置文件如下。当我从mongo读取文件时,我会以Document的形式获取数据,但是我没有任何将文件保存在excel中的功能。

在mongo中配置:

  {
    "_id" : ObjectId("5b64017c6c9b9d1a9d08563a"),
    "configId" : "1503121975430462",
    "configType" : "Workshop",
    "customerSource" : {
        "enabled" : true,
        "options" : [ 
            "Walk-in", 
            "Newspaper Ad", 
            "Print Ad", 
            "TV Ad"
        ]
    },
    "vehicleType" : {
        "enabled" : true,
        "values" : [ 
            "cars", 
            "bikes"
        ]
    },
    "createdOn" : ISODate("2018-09-06T09:47:25.006Z"),
    "updatedOn" : ISODate("2018-11-27T15:55:22.724Z")
}

//用于从配置中获取数据的功能。

`public Document GetConfig(String AccountId) throws Exception{
        String MongoColl = ExcelUtils.getCellData(7,4);
        String QueryKey = ExcelUtils.getCellData(7,5);
        //Get table/collection in variable
        MongoCollection<Document> collector = GetCollection(MongoColl);
        // Get documents by query
        BasicDBObject query = new BasicDBObject();
        query.put(QueryKey, WorkshopId);
        //Get value based on query
        MongoIterable<Document> cursor = collector.find(query);
        //Print first row of data returned
        System.out.println(cursor.first());
        return cursor.first();
    }`

//我得到的输出如下所示

`Document{{_id=1514279651131149, configId=1503121975430462, configType=Workshop,  customerSource=Document{{enabled=true, options=[Walk-in, Newspaper Ad, Print Ad, TV Ad, Leaflet]}}, vehicleType=Document{{enabled=true, values=[cars]}}, createdOn=Tue Dec 26 14:44:11 IST 2017, updatedOn=Tue Nov 27 19:11:42 IST 2018}}`

//在excel中保存数据的功能如下

    public static void setCellData(Document value, int RowNum, int ColNum) throws Exception {
         try {
            row = excelWSheet.getRow(RowNum);
            if (row == null) {
                row = excelWSheet.createRow(RowNum);
            }
            cell = row.getCell(ColNum);
            if (cell == null) {
                cell = row.createCell(ColNum);
                cell.setCellValue(value);
            } else {
                cell.setCellValue(value);
            }
            // Constant variables Test Data path and Test Data file name
            FileOutputStream fileOut = new FileOutputStream(Constant.Path_TestData + Constant.File_TestData);
            excelWBook.write(fileOut);
            fileOut.flush();
            fileOut.close();

        } catch (Exception e) {
            try {
                throw (e);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

但这会导致错误:

"The method setCellValue(double) in the type Cell is not applicable for the arguments (Document)"

我想将config中的所有值保存到Excel文件中。谁能建议如何在Excel中的不同行中以Key:value格式保存此输出(文档)?

0 个答案:

没有答案