使用jsp / servlet将excel数据上载到数据库中

时间:2015-04-13 13:41:43

标签: java excel jsp servlets

1)如何开发一个将excel中的所有数据传输到oracle数据库的软件?

2)Excel工作表中的单元格可能会根据用户提供的输入而有所不同,因此我们必须动态读取excel并根据列值创建临时表?

请建议我如何做,如果可能的话,建议我一个示例链接

提前致谢

使用浏览按钮

读取文件名
             fileName = new File(item.getName()).getName();
                    String filePath = uploadPath + File.separator + fileName;
                    //System.out.println("File Name : "+fileName);
                    //System.out.println("File Path : "+filePath);
                    File storeFile = new File(filePath);
                    // saves the file on disk
                    if(fileName != null && fileName.length()>0){
                        System.out.println("Store File : "+fileName);
                        item.write(storeFile);
                        fileInp=item.getInputStream(); 

阅读给定的Excel文件

        System.out.println("Started reading Excel File");
        String[] title = new String[6];
        String[] val = new String[6];

        workbook = new HSSFWorkbook(fileInp);

        Sheet worksheet = workbook.getSheetAt(0);
        rowCount = worksheet.getLastRowNum();
        Row row = worksheet.getRow(0);
        Cell col1 = row.getCell((short) 0);
        title[0] = col1.getStringCellValue();
        Cell col2 = row.getCell((short) 1);
        title[1] = col2.getStringCellValue();
        Cell col3 = row.getCell((short) 2);
        title[2] = col3.getStringCellValue();
        Cell col4 = row.getCell((short) 3);
        title[3] = col4.getStringCellValue();
        Cell col5 = row.getCell((short) 4);
        title[4] = col5.getStringCellValue();
        Cell col6 = row.getCell((short) 5);
        title[5] = col6.getStringCellValue();

        for (int i = 1; i <= rowCount; i++) {

            row = worksheet.getRow(i);
            Cell col;
            for (int j = 0; j < 6; j++) {
                col = row.getCell((short) j);
                if (col != null) {
                    col.setCellType(HSSFCell.CELL_TYPE_STRING);
                    val[j] = col.getStringCellValue();
                    /*
                     * switch (col.getCellType()) { case
                     * HSSFCell.CELL_TYPE_STRING: val[j] =
                     * col.getStringCellValue(); break; case
                     * HSSFCell.CELL_TYPE_NUMERIC: val[j] =
                     * col.getNumericCellValue() + ""; break; }
                     */
                }
            }

除此之外,我需要以动态方式执行相同的过程,在上面的情况下,我已经预先定义了列,但是我需要编写一个代码,其中i应该根据列的no来生成表是数据库中的der

1 个答案:

答案 0 :(得分:1)

一种可能的方法是逐行使用Apache Poi to read the excel表。

获得数据后,您可以使用JDBC驱动程序将数据填充到数据库中。