java - 上传excel文件从文件中读取数据并使用spring boot Rest api将其插入数据库

标签 java oracle11g apache-poi multipartform-data

我想开发一个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/

相关文章:

oracle - 表不存在则创建,创建后进入一行

java - 使用 JLabel 将 JTextArea 插入 JPanel

java - 在SWT环境中如何知道一个线程是否完成了它的任务?

sql - COUNT() 基于表中列的影响

oracle - Oracle 12c 中的子选择性能不佳

java - 程序停止尝试读取 Excel 工作簿 (Apache POI)

apache-poi - 如何使用 POI 读取和编辑巨大的 Excel 文件?

java - 无效行号 (-32768) 超出允许范围 (0..1048575)

java - 如何将秒表 JavaScript 中的数据存储到 MySQL

java - 如何将表单字段集合发布到 Spring Controller ?