Export Data from Excel File to a table in Database.

How to read Excel file from java:❓❓

Use APACHE POI. 

Refer online for Tutorials:http://www.tutorialspoint.com/apache_poi/


1. Install Apache POI (useful for importing and exporting data from excel files)

2. Import necessary Packages required for database connection and reading data

from the spread sheets.

3. JDBC is used to connect to database, create a table in the database for

exporting data into it.

4. Setup a File Input Stream for reading a excel file in java. (For reading a

excel file.)

5. Now get the instance of workbook and iterate all the rows in the sheet


6. Iterate every row and insert them in to database table.




Code:
(I have five fields in my excel file and i am exporting it to database.)



        int d1=0;
        String d2="",d3="",d4="",d5="";

        try

        {
            
        //Database Connection    
        // For Connection code refer online.

        System.out.println("Sucessfully connected to database");    

        
        
        //Reading an excel file.
        //String excelFilePath = "C:\\Users\\abc\\Desktop\\stu.xlsx";
        FileInputStream inputStream = new FileInputStream(new  File(excelFilePath));
        
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet firstSheet = workbook.getSheetAt(0);
        
        //extracting the values from rows in excel.
        Iterator<Row> iterator = firstSheet.iterator();                
         while (iterator.hasNext()) {
            Row nextRow = iterator.next();
            Iterator<Cell> cellIterator = nextRow.cellIterator();
             
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                
                int num=0;
                
                num=cell.getColumnIndex();                
                 
                switch (num) {
                    case 0:
                        d1=(int)cell.getNumericCellValue();
                        System.out.print(d1);                        
                        break;                   
                    case 1:
                        d2=cell.getStringCellValue();
                        System.out.print(d2);
                        break; 
                    case 2:
                        d3=cell.getStringCellValue();
                        System.out.print(d3);
                        break; 
                    case 3:
                        d4=cell.getStringCellValue();
                        System.out.print(d4);
                        break;
                    case 4:
                       if (DateUtil.isCellDateFormatted(cell)) 
                            {
                            SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd"); 
                            d5=dateFormat.format(cell.getDateCellValue());
                            System.out.print(d5);
                            }
                        break;                                                                         
                }
                System.out.print("  ");
            }
            //inserting into database.
            String sql="insert into student values(?,?,?,?,?)";
            PreparedStatement p1=conn.prepareStatement(sql);
            p1.setInt(1, d1);
            p1.setString(2, d2);
            p1.setString(3, d3);
            p1.setString(4, d4);
            p1.setString(5, d5);
            
            p1.executeUpdate();
            
            System.out.println();
        }
         
        workbook.close();
        inputStream.close();
        
        JOptionPane.showMessageDialog(null,"Sucessfully exported to database","Alert", JOptionPane.INFORMATION_MESSAGE);
        System.out.println("Successfully exported to database");
        
        }
        catch (FileNotFoundException e) {
   e.printStackTrace();
  }
        catch (IOException e) {
   e.printStackTrace();
  }
        catch(ClassNotFoundException | SQLException e){
                        e.printStackTrace();

            }


















Comments

Popular posts from this blog

FC Barcelona vs Real Madrid -El Clasico Rivalry.

1NF,2NF, 3NF Normalization

Memory Management (Contiguous and Non-Contiguous ).