IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Documents Java Discussion :

Exception ClassCast avec WritableCell dans JExcel


Sujet :

Documents Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 53
    Par défaut Exception ClassCast avec WritableCell dans JExcel
    Bonjour,

    J'ai un petit souci avec JExcel. J'ai une

    java.lang.ClassCastException: jxl.biff.EmptyCell cannot be cast to jxl.write.Label

    à la ligne où il y a :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     
    Label cat_lbl = (Label) cat_cell;
    je ne comprend pas pourquoi il me fait ca vu que je suis à la lettre la doc fournie avec l'API; voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
     
     
    	import java.io.File;
    import java.io.IOException;
    import java.util.Enumeration;
    import javax.swing.tree.DefaultMutableTreeNode;
    import jxl.*;
    import jxl.write.*;
     
    import javax.swing.*;
     
     
     
    public class XLS_Generator {
     
    	public String xlssourcepath = null;
    	public String xlssourcedestination=null;
    	public boolean success=false;
    	private int nbelements=0;
    	public Workbook sourcedoc=null;
    	public WritableWorkbook destdoc=null;
    	private WritableSheet worksheet=null;
    	public boolean proceedconfirmation=true;
    	private int i=0;
     
    	public XLS_Generator (String source, String destination, DefaultMutableTreeNode rootnode) throws WriteException, IOException {
     
    		this.xlssourcedestination=destination;
     
    		this.xlssourcepath=source;
     
     
    		try {
     
    			this.sourcedoc= Workbook.getWorkbook(new File(source));
    			this.destdoc = Workbook.createWorkbook(new File(destination),this.sourcedoc);
    			this.worksheet=this.destdoc.getSheet(0);
     
    		} 
     
    		catch (Exception e) {
     
    			JOptionPane.showMessageDialog(null, e.getMessage(),"XLS report generator",JOptionPane.ERROR_MESSAGE);
    			e.printStackTrace();
    		}
     
     
    		initialize(rootnode,0);
    		this.destdoc.write();
    		this.destdoc.write();
     
    	}
     
     
    	private boolean initialize(DefaultMutableTreeNode node, int i) throws IOException, WriteException { //this method initializes the xls report by writing the first line that corresponds to the titles of the columns in the document
     
     
     
    		if (proceedconfirmation) {
     
    				Integer level=this.i+1;
     
    				String levelmsg=level.toString();
     
    				switch (level) {
     
    				case 1 : levelmsg="first"; break;
     
    				case 2 : levelmsg="second"; break;
     
    				case 3 : levelmsg="third"; break;
     
    				default : levelmsg+="th";
     
    				}
     
     
    				int response = JOptionPane.showConfirmDialog(null, "Please select a name for your column ","XLS Generator",JOptionPane.OK_CANCEL_OPTION);
     
     
    				if (response==JOptionPane.OK_OPTION) {
     
    						if (node.getChildAt(0).isLeaf()==false) {
     
    						String generalcategory = JOptionPane.showInputDialog(null,"Name for the column");
     
    						WritableCell cat_cell = this.worksheet.getWritableCell(this.i,0);
     
    						Label cat_lbl = (Label) cat_cell;
    						cat_lbl.setString(generalcategory);
     
    						this.i++; //pour passer a la colonne suivante
     
    						Label percent_lbl = new Label(this.i,0,"%");
    						this.worksheet.addCell(percent_lbl);
     
     
     
    						i++; // to go to the next column
     
    						this.destdoc.write();
     
     
    						DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(0);
     
    						if(child.getChildAt(0).isLeaf()==false) {
     
    							initialize (child,this.i);
     
    						}
     
     
    					}						
     
    				}
     
    				if (response==JOptionPane.CANCEL_OPTION) {
     
    					JOptionPane.showMessageDialog(null, "The xls won't be generated");
    					this.proceedconfirmation=false;
     
    				}
     
     
     
     
     
    		}
     
     
     
    		return proceedconfirmation;
     
    	}
     
    }

    juste pour info : cette classe me permet en fait de reproduire la hierarchie d'un jtree dans un fichier excel ... j'en suis qu'au début j'ai pas encore fini l'algo mais là ca coince deja comme vous le voyez ...



    merci pour le coup de main ...

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 53
    Par défaut
    bon apres relecture de mon code je me suis rendu compte d'une betise que j'ai faite ...

    je n'ai plus d'exception maintenant mais j'ai toujours un souci, le resultat retourné n'est pas le bon ... il ne m'ecrit que dans deux cellules alors que ma boucle s'execute autant de fois que je veux ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
     
     
     
    	import java.io.File;
    import java.io.IOException;
    import java.util.Enumeration;
    import javax.swing.tree.DefaultMutableTreeNode;
    import jxl.*;
    import jxl.write.*;
     
    import javax.swing.*;
     
     
     
    public class XLS_Generator {
     
    	public String xlssourcepath = null;
    	public String xlssourcedestination=null;
    	public boolean success=false;
    	private int nbelements=0;
    	public Workbook sourcedoc=null;
    	public WritableWorkbook destdoc=null;
    	private WritableSheet worksheet=null;
    	public boolean proceedconfirmation=true;
    	private int i=0;
     
    	public XLS_Generator (String source, String destination, DefaultMutableTreeNode rootnode) throws WriteException, IOException {
     
    		this.xlssourcedestination=destination;
     
    		this.xlssourcepath=source;
     
     
    		try {
     
    			this.sourcedoc= Workbook.getWorkbook(new File(source));
    			this.destdoc = Workbook.createWorkbook(new File(destination),this.sourcedoc);
    			this.worksheet=this.destdoc.getSheet(0);
     
    		} 
     
    		catch (Exception e) {
     
    			JOptionPane.showMessageDialog(null, e.getMessage(),"XLS report generator",JOptionPane.ERROR_MESSAGE);
    			e.printStackTrace();
    		}
     
     
    		initialize(rootnode,0);
    		this.destdoc.write();
    		this.destdoc.close();
     
    	}
     
     
    	private boolean initialize(DefaultMutableTreeNode node, int i) throws IOException, WriteException { //this method initializes the xls report by writing the first line that corresponds to the titles of the columns in the document
     
     
     
    		if (proceedconfirmation) {
     
    				Integer level=this.i+1;
     
    				String levelmsg=level.toString();
     
    				switch (level) {
     
    				case 1 : levelmsg="first"; break;
     
    				case 2 : levelmsg="second"; break;
     
    				case 3 : levelmsg="third"; break;
     
    				default : levelmsg+="th";
     
    				}
     
     
    				int response = JOptionPane.showConfirmDialog(null, "Please select a generic name for your " +levelmsg + "-level categories","XLS Generator",JOptionPane.OK_CANCEL_OPTION);
     
     
    				if (response==JOptionPane.OK_OPTION) {
     
    						if (node.getChildAt(0).isLeaf()==false) {
     
    						String generalcategory = JOptionPane.showInputDialog(null,"Name for " +levelmsg +" level-category name:");
     
    					//	WritableCell cat_cell = this.worksheet.getWritableCell(this.i,25);
     
    					//	Label cat_lbl = (Label) cat_cell;
    						//cat_lbl.setString(generalcategory);
     
     
    						this.worksheet.addCell(new Label(i,25,generalcategory));
     
     
    						this.worksheet.addCell(new Label(i+1,25,"%"));
     
     
    						this.destdoc.write();
     
     
    						DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(0);
     
    						if(child.getChildAt(0).isLeaf()==false) {
     
    							initialize (child,i+2);
     
    						}
     
     
    					}
     
     
    					}						
     
    				}
     
    				if (response==JOptionPane.CANCEL_OPTION) {
     
    					JOptionPane.showMessageDialog(null, "The xls won't be generated");
    					this.proceedconfirmation=false;
     
    				}
     
     
     
     
     
    		}
     
     
     
    		return proceedconfirmation;
     
    	}
     
    }

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 53
    Par défaut
    je met ici un code simplifié de ce qui se passe ... en fait quand j'execute ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
     
    Workbook source = Workbook.getWorkbook(new File("C:/Data/meavy/myxls.xls"));
     
    WritableWorkbook dest = Workbook.createWorkbook(new File("C:/Data/meavy/myxls2.xls"), source);
     
    			WritableSheet sheet = dest.getSheet(0);
     
                            for (int i = 0; i < 3; i++) {
     
    				sheet.addCell(new Label(0+i, 25, "code"));
    				sheet.addCell(new Label(1+i, 25, "couleur"));
     
    			}
    			dest.write();
    			dest.close();

    il me sort effectivement 3 colonnes code mais seulement une seule colonne couleur !!

    code | code | code | couleur

    alors que mon code est sensé en créer une alternance des deux :

    code | couleur | code | couleur | code | couleur

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 53
    Par défaut
    bon eh bien non ... c'est simple c'est une erreur d'utilisation de ma part ... avis aux futurs utilisateurs débutants de JExcel ....

    Dans JExcel , il est PRIMORDIAL de n'appeler la methode write() qu'une fois que l'edition des cellules est finie !!

    mon erreur était donc de faire ca

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
    mafeuille.addcell(cellule1);
    mafeuille.write();
     
    mafeuille.addcell(cellule2);
    mafeuille.write();
    il faut plutot faire ceci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
     
    mafeuille.addcell(cellule1);
    mafeuille.addcell(cellule2);
     
    mafeuille.write();

    voilà !!!

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2007
    Messages : 97
    Par défaut
    Rhan, mille merci ! Je faisais aussi la même erreur... Deux jours que j'étais dessus !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 24/03/2010, 15h30
  2. Réponses: 5
    Dernier message: 30/04/2008, 23h39
  3. Nouvelle form avec bouton dans barre taches
    Par DarkChamallo dans le forum API, COM et SDKs
    Réponses: 3
    Dernier message: 17/05/2004, 20h44
  4. [VB6] Multiselection avec CTRL dans une msflexgrid
    Par Troopers dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 17/04/2003, 10h57
  5. PB avec "self" dans une dll
    Par DiJiRiDouS dans le forum Langage
    Réponses: 2
    Dernier message: 21/02/2003, 09h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo