我想开发一个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>
提前致谢。
答案 0 :(得分:1)
您将需要您的连接字符串,以及执行插入的存储过程(并且可能返回令牌或可能是新的唯一标识符):
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