簡體   English   中英

使用 Java 將 Excel 數據導入 Mongodb

[英]Import Excel Data to Mongodb using Java

嘗試以以下文檔格式將 Excel 數據導入 Mongo db

[
{"productId":"",
"programeName":"",
"programeThumbImageURL":"",
"programeURL":"",
"programEditors":["editor1","editor2"],
"programChapters":[
{
"chapterName":"chapter1",
"authorNames":["authorName1","authorname2"]
},
{"chapterName":"chapter2"},
"authorNames":["authorName1","authorName2"]
}
,...
]},...]

Excel 中有許多產品帶有chapterNames 有多個作者。 以下是嘗試執行的代碼,我可以插入數據。 但是我無法合並與上述特定章節名稱相對應的作者名稱。 所以目前有 programChapters 數組包含對象作為重復的chapterNames。 以下代碼顯示了我對此的實驗。

private static XSSFWorkbook myWorkBook;

public static void main(String[] args) throws IOException {

String[] programs = {"programName1","programName2","programName3","programName4",...};

@SuppressWarnings("deprecation")
Mongo mongo = new Mongo("localhost", 27017);
@SuppressWarnings("deprecation")
DB db = mongo.getDB("dbName");

DBCollection collection = db.getCollection("programsCollection");

File myFile =
    new File("dsm_article_author_details.xlsx");
FileInputStream fis = new FileInputStream(myFile); // Finds the workbook instance for XLSX file
myWorkBook = new XSSFWorkbook(fis);
XSSFSheet mySheet = myWorkBook.getSheetAt(0); // Get iterator to all the rows in current sheet

@SuppressWarnings("unused")
Iterator<Row> rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file
for (String program : programs) {
  String programName = "";
  String chapterName = "";
  String authorName = "";

  BasicDBObject product = new BasicDBObject();

  BasicDBList programChaptersList = new BasicDBList();
  // For Each Row , Create Chapters Object here

  for (int i = 0; i <= mySheet.getLastRowNum(); i++) { // points to the starting of excel i.e
                                                       // excel first row
    Row row = (Row) mySheet.getRow(i); // sheet number
    System.out.println("Row is :" + row.getRowNum());

    BasicDBObject programChapters = new BasicDBObject();
    if (row.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
      programName = row.getCell(0).getStringCellValue();
      System.out.println("programName : " + programName);
    }

    if (row.getCell(1).getCellType() == Cell.CELL_TYPE_STRING) {
      chapterName = row.getCell(1).getStringCellValue().replaceAll("\n", "");
      System.out.println("chapterName : " + chapterName);
    }

    if (row.getCell(2).getCellType() == Cell.CELL_TYPE_STRING) {
      authorName = row.getCell(2).getStringCellValue();
      System.out.println("authorName : " + authorName);
    }

    List<String> authors = new ArrayList<String>();

    programChapters.put("chapterName", chapterName);

    authors.add(authorName);
    programChapters.put("authorName", authors);

    if (programName.trim().equals(program.trim())) {
      programChaptersList.add(programChapters);
    }
  }

  product.put("programName", program);
  product.put("programThumbImageURL", "");
  product.put("programeURL", "");
  product.put("programChapters", programChaptersList);

  collection.insert(product);
  System.out.println("*#*#*#*#*#");
}
}

我希望這是出錯的部分。 需要將所有chapterNames 存儲在一個數組中並與每個即將到來的值進行比較,並根據該值創建新對象並將其存儲在一個列表中

List<String> authors = new ArrayList<String>();

programChapters.put("chapterName", chapterName);

authors.add(authorName);
programChapters.put("authorName", authors);

有人可以建議我,可用的解決方案:-)

我希望這是出錯的部分。 需要將所有chapterNames 存儲在一個數組中並與每個即將到來的值進行比較,並根據該值創建新對象並將其存儲在一個列表中

List<String> authors = new ArrayList<String>();

programChapters.put("chapterName", chapterName);

authors.add(authorName);
programChapters.put("authorName", authors);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM