Ouverture/lecture d'un fichier xls

Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008 - 29 juil. 2008 à 17:09
Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008 - 4 août 2008 à 09:48
Bonjours.

Donc voila... Je cherche a récupérer des données de type string (text) dans un fichier XLS a partir d'une apli JAVA.

Mais voila... J'ai un message d'erreur comme quoi l'ouverture du fichier xls a échoué...

Pour infos j'utilise la librairie jxl.

Voila la bride de code la ou sa ne marche pas :

//    #######################################################################
    public void generer(){

        try{
            WritableWorkbook workbook = Workbook.createWorkbook(new File(sDirectory));
            Sheet sheet = workbook.getSheet(2);
            Cell b = sheet.getCell(0,2);
            if (b.getType() == CellType.LABEL)
            {
              LabelCell lc = (LabelCell) b;
              a = lc.getString();
            }           
            workbook.close();
            jLabel1.setText("Test: " + b);
            /*jLabel1.setText("Génération terminer avec succès " + b + " lignes générer.");*/
            jButton4.setEnabled(true);
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);}
       
        catch(Exception ex) {
        JOptionPane.showMessageDialog(null, "Impossible d'ouvrir le fichier xls", "Erreur", JOptionPane.ERROR_MESSAGE);

        jProgressBar1.setValue(0);
        }
    }
}

Merci d'avance de votre aide.

Ps: si pour une meilleur compréhension vous avais besoin de la totalité du code faite le moi savoir.
Impossibeulman.

7 réponses

Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008
30 juil. 2008 à 09:48
UP ?!
Plus de précisions...
J'ai modifier légèrement mon code:






    public void OpenExcel() {
        if (jfcChooser == null)
        {
            jfcChooser = new JFileChooser();
            jfcChooser.addChoosableFileFilter(new xlsFilter());
        }
        int fileState = jfcChooser.showOpenDialog(null);
        File file = jfcChooser.getSelectedFile();        if (file !null && fileState JFileChooser.APPROVE_OPTION)
        {
            setFilePath(file.getAbsolutePath());
        }
        else if (fileState == JFileChooser.CANCEL_OPTION)
        {
            bIsFile = false;
        }
    }
    public String getFilePath() {
        return sDirectory;
    }
    public boolean isFileSelected() {
        return bIsFile;
    }
    public void setFilePath(String fpath1) {
        sDirectory = fpath1;
        bIsFile = true;
        jLabel2.setText(fpath1);
            //ICI "fpath1" retourne bien une valeur string (D:\Documents and Settings\florian.murail\My Documents\nom_fichier.xls)

        jButton2.setEnabled(true);
        jButton4.setEnabled(false);
        jProgressBar1.setValue(0);
    }

//    #######################################################################


    public String a;
    public String b;
    public String c;
    public String d;
    private File fpath1;


//    #######################################################################


    public void generer(){

        try{


            /*JOptionPane.showMessageDialog(null,"$-Test terminer...", "TEST", JOptionPane.INFORMATION_MESSAGE);*/


            WritableWorkbook source = Workbook.createWorkbook(fpath1);
            WritableSheet sheet1 = source.createSheet(a, 2);


            //traitement


             (...................................)
            source.write();
            source.close();
            jButton4.setEnabled(true);
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);}


        /*catch(Exception ex) {
        JOptionPane.showMessageDialog(null, "Impossible d'ouvrir le fichier xls", "Erreur", JOptionPane.ERROR_MESSAGE);*/


        catch (Exception e) {
            e.printStackTrace();
        jProgressBar1.setValue(0);
        }
    }


//    #######################################################################



Et voici l'erreur que me retourne "catch (Exception e)"
:

java.lang.NullPointerException
    at java.io.FileOutputStream.(Unknown Source)
    at java.io.FileOutputStream.(Unknown Source)
    at jxl.Workbook.createWorkbook(Workbook.java:301)
    at jxl.Workbook.createWorkbook(Workbook.java:286)
    at Apli.SwingApli.generer(SwingApli.java:250)
    at Apli.SwingApli$6.jButton3_clic(SwingApli.java:156)
    at Apli.SwingApli$6.actionPerformed(SwingApli.java:155)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

----------------------------------------------------------------------------

Donc semblerais que le souci viens du faite que sa trouve pas le fichier...
Sa fait seulement 1 mois que j'ai commencer le java, merci d'avent pour votre aide,
quel qu'il soit...

De plus il ne me reste plus beaucoup de temps pour réaliser mon mini_projet...

Cordialement.

Impossibeulman.
0
Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008
30 juil. 2008 à 09:48
UP ?!
Plus de précisions...
J'ai modifier légèrement mon code:






    public void OpenExcel() {
        if (jfcChooser == null)
        {
            jfcChooser = new JFileChooser();
            jfcChooser.addChoosableFileFilter(new xlsFilter());
        }
        int fileState = jfcChooser.showOpenDialog(null);
        File file = jfcChooser.getSelectedFile();        if (file !null && fileState JFileChooser.APPROVE_OPTION)
        {
            setFilePath(file.getAbsolutePath());
        }
        else if (fileState == JFileChooser.CANCEL_OPTION)
        {
            bIsFile = false;
        }
    }
    public String getFilePath() {
        return sDirectory;
    }
    public boolean isFileSelected() {
        return bIsFile;
    }
    public void setFilePath(String fpath1) {
        sDirectory = fpath1;
        bIsFile = true;
        jLabel2.setText(fpath1);
            //ICI "fpath1" retourne bien une valeur string (D:\Documents and Settings\florian.murail\My Documents\nom_fichier.xls)

        jButton2.setEnabled(true);
        jButton4.setEnabled(false);
        jProgressBar1.setValue(0);
    }

//    #######################################################################


    public String a;
    public String b;
    public String c;
    public String d;
    private File fpath1;


//    #######################################################################


    public void generer(){

        try{


            /*JOptionPane.showMessageDialog(null,"$-Test terminer...", "TEST", JOptionPane.INFORMATION_MESSAGE);*/


            WritableWorkbook source = Workbook.createWorkbook(fpath1);
            WritableSheet sheet1 = source.createSheet(a, 2);


            //traitement


             (...................................)
            source.write();
            source.close();
            jButton4.setEnabled(true);
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);}


        /*catch(Exception ex) {
        JOptionPane.showMessageDialog(null, "Impossible d'ouvrir le fichier xls", "Erreur", JOptionPane.ERROR_MESSAGE);*/


        catch (Exception e) {
            e.printStackTrace();
        jProgressBar1.setValue(0);
        }
    }


//    #######################################################################



Et voici l'erreur que me retourne "catch (Exception e)"
:

java.lang.NullPointerException
    at java.io.FileOutputStream.(Unknown Source)
    at java.io.FileOutputStream.(Unknown Source)
    at jxl.Workbook.createWorkbook(Workbook.java:301)
    at jxl.Workbook.createWorkbook(Workbook.java:286)
    at Apli.SwingApli.generer(SwingApli.java:250)
    at Apli.SwingApli$6.jButton3_clic(SwingApli.java:156)
    at Apli.SwingApli$6.actionPerformed(SwingApli.java:155)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

----------------------------------------------------------------------------

Donc semblerais que le souci viens du faite que sa trouve pas le fichier...
Sa fait seulement 1 mois que j'ai commencer le java, merci d'avent pour votre aide,
quel qu'il soit...

De plus il ne me reste plus beaucoup de temps pour réaliser mon mini_projet...

Cordialement.

Impossibeulman.
0
indiana_jules Messages postés 750 Date d'inscription mardi 9 mars 2004 Statut Membre Dernière intervention 23 décembre 2008 22
30 juil. 2008 à 13:41
Salut,
Lis bien l'erreur retournée. Que dit-elle ? Qu'un "NullPointerException" a été levée. Autrement dit, cela ne vient pas du fait que le fichier lui est inconnu, mais qu'une variable de ton programme n'est pas iniatilisée alors que le programme doit faire une opération dessus.

Or quand on regarde ton code, on a "WritableWorkbook source = Workbook.createWorkbook(fpath1);
" avec aucune initialisation de la variable fpath1 de type File
Du coup, je te conseille de faire cela:

File file = jfcChooser.getSelectedFile();        if (file !null && fileState JFileChooser.APPROVE_OPTION)
        {
            fpath1 = file;
            setFilePath(file.getAbsolutePath());
        }
        else if (fileState == JFileChooser.CANCEL_OPTION)
        {
            bIsFile = false;
        }

Voili voilà

le monde a des idées : la preuve, c'est qu'il y en a de mauvaises
ne comprends pas tout, mais je parle de tout : c'est ce qui compte
0
Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008
30 juil. 2008 à 17:31
Sa ne semble pas marcher ...
















                                                      Erreur:












Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Apli.SwingApli.OpenExcel(SwingApli.java:235)
    at Apli.SwingApli$4.jButton1_clic(SwingApli.java:142)
    at Apli.SwingApli$4.actionPerformed(SwingApli.java:141)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


---------------------------------------------------------------------------

Le code actuel:

//    #######################################################################   
    public static JFileChooser
jfcChooser1 = null;
    public String
sDirectory1;
    public boolean
bIsFile1 = false;
    private File
fpath1;

//    #######################################################################   
    public void OpenExcel() {
        File file =
jfcChooser1.getSelectedFile();        if (file !null && fileState JFileChooser.APPROVE_OPTION)
        {  fpath1 = file;
            setFilePath1(file.getAbsolutePath()); }
        else if (fileState == JFileChooser.CANCEL_OPTION)
        { bIsFile1 = false; }
    }

    public String getFilePath1() {
        return
sDirectory1; }
    public boolean isFileSelected1() {
        return
bIsFile1; }
    public void setFilePath1(String
fpath1) {
       
sDirectory1 =
fpath1;
       
bIsFile1 = true;
        jLabel3.setText(
fpath1);
        jButton3.setEnabled(true);
        JOptionPane.showMessageDialog(null,
fpath1, "TEST", JOptionPane.INFORMATION_MESSAGE);
//test
    }

Merci d'avance
Cordialement.
0

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

Posez votre question
indiana_jules Messages postés 750 Date d'inscription mardi 9 mars 2004 Statut Membre Dernière intervention 23 décembre 2008 22
30 juil. 2008 à 20:02
A quelle ligne de ton code cela correspond-t-il ? : at Apli.SwingApli.OpenExcel(SwingApli.java:235)

Parce que l'erreur est la même, tu utilises une variable non initialisée (PS: évite au plus le static: il faut vraiement se servir de ce mot clé uniquement pour les constantes et pour les singleton, car sinon, cela peut amener à des erreurs)

Voili voilà

le monde a des idées : la preuve, c'est qu'il y en a de mauvaises
ne comprends pas tout, mais je parle de tout : c'est ce qui compte
0
Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008
31 juil. 2008 à 17:00
Je crois que mon code est obsolète...

Si tu devait chercher a Ouvrir un fichier xls avec une OpenDialog , qui permet de savoir ou se trouve le fichier a exploiter.
(exemple c:/toto/tata/le_fichier.xls)
Puis
(après l'appui sur un bouton "Générer"), sa ouvre le fichier exel (en processus cacher),affin de faire plusieurs opération comme compter les
lignes, récupérer des données a stoker dans un tableau etc...

Merci d'avance.

Cordialement.

Impossibeulman.
0
Impossibeulman Messages postés 32 Date d'inscription jeudi 7 février 2008 Statut Membre Dernière intervention 25 août 2008
4 août 2008 à 09:48
UP



Impossibeulman.
0
Rejoignez-nous