如何使用spring boot从excel中读取数据并将其存储在mongodb中
我想开发一个代码,在这里我可以从excel中读取数据,并使用spring mvc spring boot将数据存储在mongodb中。另外,在我的项目中,我们已经分离了数据库层,所以我通过URL使用mongodb API。Mongodb托管在AWS上如何使用spring boot从excel中读取数据并将其存储在mongodb中,spring,mongodb,spring-boot,spring-mvc,apache-poi,Spring,Mongodb,Spring Boot,Spring Mvc,Apache Poi,我想开发一个代码,在这里我可以从excel中读取数据,并使用spring mvc spring boot将数据存储在mongodb中。另外,在我的项目中,我们已经分离了数据库层,所以我通过URL使用mongodb API。Mongodb托管在AWS上 我已尝试读取excel文件,但无法将其存储在mongodb中。您提供的相关数据非常少,这将使我们难以帮助您,但我尝试 要从excel中读取数据,需要在代码库中进行代码更改 将以下依赖项添加到pom.xml文件中 直接进入问题的商务部分,在课堂上写下
我已尝试读取excel文件,但无法将其存储在mongodb中。您提供的相关数据非常少,这将使我们难以帮助您,但我尝试 要从excel中读取数据,需要在代码库中进行代码更改 将以下依赖项添加到pom.xml文件中 直接进入问题的商务部分,在课堂上写下以下内容
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class ExcelReader {
public static final String SAMPLE_XLSX_FILE_PATH = "./sample-xlsx-file.xlsx";
public static void main(String[] args) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
/*
=============================================================
Iterating over all the sheets in the workbook (Multiple ways)
=============================================================
*/
// 1. You can obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
}
// 2. Or you can use a for-each loop
System.out.println("Retrieving Sheets using for-each loop");
for(Sheet sheet: workbook) {
System.out.println("=> " + sheet.getSheetName());
}
// 3. Or you can use a Java 8 forEach with lambda
System.out.println("Retrieving Sheets using Java 8 forEach with lambda");
workbook.forEach(sheet -> {
System.out.println("=> " + sheet.getSheetName());
});
/*
==================================================================
Iterating over all the rows and columns in a Sheet (Multiple ways)
==================================================================
*/
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// 1. You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
// 2. Or you can use a for-each loop to iterate over the rows and columns
System.out.println("\n\nIterating over Rows and Columns using for-each loop\n");
for (Row row: sheet) {
for(Cell cell: row) {
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
// 3. Or you can use Java 8 forEach loop with lambda
System.out.println("\n\nIterating over Rows and Columns using Java 8 forEach with lambda\n");
sheet.forEach(row -> {
row.forEach(cell -> {
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
});
System.out.println();
});
// Closing the workbook
workbook.close();
}
}
就这么叫吧
为了帮助我们向mongoDB添加相同的内容,您必须向我们提供更多信息。如果您需要上述代码中的任何帮助,请进行注释。请显示您尝试过的内容。如果您尝试读取excel文件,但无法将其存储在mongodb中,那么您已经从excel读取了数据。那么,到底是什么阻碍了您将这些数据存储到数据库中呢?
compile "org.apache.poi:poi:3.17"
compile "org.apache.poi:poi-ooxml:3.17"
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class ExcelReader {
public static final String SAMPLE_XLSX_FILE_PATH = "./sample-xlsx-file.xlsx";
public static void main(String[] args) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
/*
=============================================================
Iterating over all the sheets in the workbook (Multiple ways)
=============================================================
*/
// 1. You can obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
}
// 2. Or you can use a for-each loop
System.out.println("Retrieving Sheets using for-each loop");
for(Sheet sheet: workbook) {
System.out.println("=> " + sheet.getSheetName());
}
// 3. Or you can use a Java 8 forEach with lambda
System.out.println("Retrieving Sheets using Java 8 forEach with lambda");
workbook.forEach(sheet -> {
System.out.println("=> " + sheet.getSheetName());
});
/*
==================================================================
Iterating over all the rows and columns in a Sheet (Multiple ways)
==================================================================
*/
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// 1. You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
// 2. Or you can use a for-each loop to iterate over the rows and columns
System.out.println("\n\nIterating over Rows and Columns using for-each loop\n");
for (Row row: sheet) {
for(Cell cell: row) {
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
// 3. Or you can use Java 8 forEach loop with lambda
System.out.println("\n\nIterating over Rows and Columns using Java 8 forEach with lambda\n");
sheet.forEach(row -> {
row.forEach(cell -> {
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
});
System.out.println();
});
// Closing the workbook
workbook.close();
}
}
private static void printCellValue(Cell cell) {
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case STRING:
System.out.print(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(cell.getDateCellValue());
} else {
System.out.print(cell.getNumericCellValue());
}
break;
case FORMULA:
System.out.print(cell.getCellFormula());
break;
case BLANK:
System.out.print("");
break;
default:
System.out.print("");
}
System.out.print("\t");
}
sheet.forEach(row -> {
row.forEach(cell -> {
printCellValue(cell);
});
System.out.println();
});