Copier des colonnes a partir de excel vers access en passant par netbeans

pink90 Messages postés 34 Date d'inscription lundi 25 avril 2011 Statut Membre Dernière intervention 18 juin 2013 - 30 mai 2011 à 16:43
pink90 Messages postés 34 Date d'inscription lundi 25 avril 2011 Statut Membre Dernière intervention 18 juin 2013 - 3 juin 2011 à 09:50
Salut tout le monde
s'il vous plait est ce qu'il existe une façon pour copier des colonnes d'un fichier excel vers une table dans Access en cliquant sur un bouton? ce fichier excel est importé à l'aide de filechooser

merci

6 réponses

Utilisateur anonyme
1 juin 2011 à 10:50
Bonjour

Passe par l'API Apache POI pour lire le fichier Excel. Ensuite, utilise JDBC pour te connecter à la base de données Access, il y a des exemples ici même. Une fois connecté à ta base, tu peux insérer les données lues dans le fichier Excel dans tes tables. Tu peux déclencher tout cela dans un ActionListener placé sur ton JButton. Bon courage.


























T.U.E.R yeah! vive java
0
pink90 Messages postés 34 Date d'inscription lundi 25 avril 2011 Statut Membre Dernière intervention 18 juin 2013
1 juin 2011 à 13:10
Merci pour votre réponse j'ai essayé de faire le travail avec jExcel mais ça marche pas je ne sait pas ou se trouve l'erreur
voici le code :
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          file1= jTextField1.getText(); 
          String Numero="",Numero2="";double Cout =0; res=0;
           java.util.Date date=null;
           SimpleDateFormat sdt = new SimpleDateFormat("dd/MM/yyyy");         
            Workbook WbFichier=null;
            try 
            {
                WbFichier = Workbook.getWorkbook(new File(file1));
                Sheet ShtFeuille = WbFichier.getSheet(0); 
                int nombre =ShtFeuille.getRows();
                for(int i=2;i<=nombre;i++)
                {   String columnC ="C"+i;
                    String columnD ="D"+i;
                    String columnF ="F"+i;
                    String columnH ="H"+i;
                    
                    
                    Cell cel04 = ShtFeuille.getCell(columnC); 		
                    if (cel04.getType() == CellType.LABEL) 
                    { 
                            LabelCell lc = (LabelCell) cel04; 
                           Nom = lc.getString(); 
                    }
                    Cell cel02 =ShtFeuille.getCell(columnD);
                    System.out.println(cel02.getType());
                    if (cel02.getType() == CellType.LABEL)
                    {
                        LabelCell lc = (LabelCell) cel02; 
                            Numero2 = lc.getString(); 
                            try
                            {
                                date= sdt.parse(Numero2);
                                sqlDate = new java.sql.Date(date.getTime());

                            } 
                            catch (ParseException ex) 
                            {
                                Logger.getLogger(Consultation.class.getName()).log(Level.SEVERE, null, ex);
                            }
                    }
                    Cell cel01 = ShtFeuille.getCell(columnF); 		
                    if (cel01.getType() == CellType.LABEL) 
                    { 
                            LabelCell lc = (LabelCell) cel01; 
                            Numero = lc.getString(); 
                    }
                    Cell cel03 = ShtFeuille.getCell(columnH); 		
                    if (cel03.getType() == CellType.NUMBER) 
                    { 
                           NumberCell lc = (NumberCell) cel03; 
                            Cout = lc.getValue(); 
                    }
                    
                    
                    System.out.println("aaaaaaa");
                    
                    try{    
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection con=DriverManager.getConnection("jdbc:odbc:tester");
                    Statement stmt=con.createStatement();
                    PreparedStatement ps=con.prepareStatement("INSERT INTO    Eagle(Occupant,Date_,N_Appele,Cout) VALUES("+Nom+",'"+sqlDate+"',"+Numero+","+Cout+")");
                    ps.executeUpdate();
                    ps.close();
                    con.close();
                    }
                catch(Exception exe){exe.printStackTrace();}    
                 }
             }
             catch (Exception e){e.printStackTrace();}
                  
0
Utilisateur anonyme
1 juin 2011 à 13:50
Sans trace d'exception, ça va être difficile de t'aider. Essaie d'utiliser le débogueur de Netbeans pour savoir ce qui se passe à l'exécution.









T.U.E.R yeah! vive java
0
pink90 Messages postés 34 Date d'inscription lundi 25 avril 2011 Statut Membre Dernière intervention 18 juin 2013
1 juin 2011 à 15:02
voici les erreurs qu'il affiche avec la trace :

Warning:  Usage of a local non-builtin name
Label
gggggg
java.sql.SQLException: [Microsoft][Pilote ODBC Microsoft Access] Trop peu de param?tres. 1 attendu.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3149)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:216)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPreparedStatement.java:138)
at pfe2.Consultation.jButton1ActionPerformed(Consultation.java:320)
at pfe2.Consultation.access$500(Consultation.java:17)
at pfe2.Consultation$6.actionPerformed(Consultation.java:128)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Utilisateur anonyme
1 juin 2011 à 19:37
L'erreur est là :
PreparedStatement ps=con.prepareStatement("INSERT INTO    Eagle(Occupant,Date_,N_Appele,Cout) VALUES("+Nom+",'"+sqlDate+"',"+Numero+","+Cout+")");


C'est plutôt ça :
PreparedStatement ps=con.prepareStatement("INSERT INTO    Eagle(Occupant,Date_,N_Appele,Cout) VALUES("'+Nom+'",'"+sqlDate+"',"+Numero+","+Cout+")");






T.U.E.R yeah! vive java
0
pink90 Messages postés 34 Date d'inscription lundi 25 avril 2011 Statut Membre Dernière intervention 18 juin 2013
3 juin 2011 à 09:50
Merci beaucoup pour votre aide ça a marché
mais pour le Numéro Appelé (N_Appele)il l'affiche dans Access sans le zéro au début
même si j'ai changé le type en texte.que doit-je faire ?
Merci encore une fois pour votre aide
0
Rejoignez-nous