我想开发一个代码,在其中我可以从Excel中读取数据,并使用spring mvc spring boot将数据存储在mongodb中。另外,在我的项目中,我们分离了数据库层,因此我通过URL使用了mongodb API。 Mongodb托管在AWS上。
我尝试读取excel文件,但无法将其存储在mongodb中
答案 0 :(得分:1)
您提供的问题数据很少,这将使我们很难为您提供帮助,但这是我的尝试。...
要从excel后续代码中读取数据,需要在代码库中完成
。在您的pom.xml
文件中添加以下依赖项
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
如果您使用gradle
文件后紧跟build.gradle
文件
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();
}
}
通过CellType检索单元格值
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();
});
要帮助我们将其添加到mongoDB中,您必须向我们提供更多信息。如果您需要上述代码中的任何帮助,请发表评论。