gpt4 book ai didi

java - 使用Java将Excel数据导入Mongodb

转载 作者:行者123 更新时间:2023-12-01 06:11:44 25 4
gpt4 key购买 nike

尝试以以下文档格式将 Excel 数据导入 Mongo db

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

Excel 中有许多带有 ChapterNames 的产品有多个作者。以下是尝试执行的代码,我可以插入数据。但是我无法合并与上面的特定chapterName相对应的authorNames。因此,目前有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);

关于java - 使用Java将Excel数据导入Mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33362981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com