我想开发一个rest api,它将excel工作表作为多部分对象读取文件中的内容并将其上传到oracle数据库。 到目前为止我所做的是:
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA)
public void uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
try {
ExcelController ex=new ExcelController();
File f1=ex.convert(file);
FileInputStream excelFile = new FileInputStream(f1);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + "--");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + "--");
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
依赖性:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
提前致谢。
最佳答案
您将需要连接字符串,以及执行插入操作的存储过程(并可能返回 token 或新的唯一标识符):
String sp = "{call spInsertRecord(?, ?)}";
try (Connection con = /* you get your connection here*/) {
if (con != null) {
try (CallableStatement cStatement = con.prepareCall(sp)) {
if (currentCell.getCellTypeEnum() == CellType.STRING)
cStatement.setLong("columnString", currentCell.getStringCellValue());
else if (currentCell.getCellTypeEnum() == CellType.NUMERIC)
cStatement.setLong("columnLong", currentCell.getNumericCellValue());
try (ResultSet rs = cStatement.executeQuery()) {
if (rs != null) {
// do something with your return value if you have any
rs.close();
} else {
log.error("Exception in myMethod: [ResultSet is null]");
}
}
cStatement.close();
}
} else {
log.error("Exception in myMethod.getConnection(): [Connection is null]");
}
}
您可以找到有关如何创建 Oracle 连接的更多信息 here
关于java - 上传excel文件从文件中读取数据并使用spring boot Rest api将其插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43743594/