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

API standards et tierces Java Discussion :

Java Excel API


Sujet :

API standards et tierces Java

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2012
    Messages
    277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 277
    Points : 126
    Points
    126
    Par défaut Java Excel API
    Bonjour,

    J'aimerais savoir comment je peux faire pour créer un fichier excel à partir d'un model existant et mettre certaine ligne en couleur.

    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
    public void recupereOngletExcelCourant(Integer numeroOnglet, WritableWorkbook outWorkbook,
    			Integer numeroColonneRef, Map<String, String> resultatProduitPresentDansLesDeuxExcels) {
    		// Récupération de l'onglet courant (le premier onglet)
    		WritableSheet out = outWorkbook.getSheet(numeroOnglet);
     
    		// WritableFont couleur = new WritableFont(WritableFont.ARIAL, 10, null,
    		// false, null, Colour.GREEN);
    		// WritableCellFormat couleurFormat = new WritableCellFormat(couleur);
    		for (int i = 0; i < resultatProduitPresentDansLesDeuxExcels.size(); i++) {
    			Cell cellule = out.getCell(numeroColonneRef, i); // cols,
    																		// rows
    			if (resultatProduitPresentDansLesDeuxExcels.containsKey(cellule.getContents())) {
    				cellule.getContents();
    				// out.setColumnView(numeroColonneRef, i,
    				// couleurFormat);
    			}
    		}
     
    		// Toutes les cellules sont remplies :
    		// Sauvegarde du fichier
    		try {
    			outWorkbook.write();
    			outWorkbook.close();
    		} catch (IOException | WriteException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    Je vous ai mis un bout de code afin de voir un peu mon chemin dans cette méthode, c'est dans celle-ci que je voudrais définir une couleur pour une ligne.

    Quand j'exécute le programme il me plante sur outWorkbook.close(); du code précédent. Avec cette erreur :
    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
    jxl.common.AssertionFailed
    	at jxl.common.Assert.verify(Assert.java:37)
    	at jxl.write.biff.CompoundFile.readAdditionalPropertySets(CompoundFile.java:398)
    	at jxl.write.biff.CompoundFile.<init>(CompoundFile.java:209)
    	at jxl.write.biff.File.close(File.java:116)
    	at jxl.write.biff.WritableWorkbookImpl.close(WritableWorkbookImpl.java:456)
    	at fr.gui.metier.CreationFichierExcel.recupereOngletExcelCourant(CreationFichierExcel.java:88)
    	at fr.gui.execute.execute.main(execute.java:84)
    Exception in thread "main" jxl.common.AssertionFailed
    	at jxl.common.Assert.verify(Assert.java:37)
    	at jxl.write.biff.CompoundFile.readAdditionalPropertySets(CompoundFile.java:398)
    	at jxl.write.biff.CompoundFile.<init>(CompoundFile.java:209)
    	at jxl.write.biff.File.close(File.java:116)
    	at jxl.write.biff.WritableWorkbookImpl.close(WritableWorkbookImpl.java:456)
    	at fr.gui.metier.CreationFichierExcel.recupereOngletExcelCourant(CreationFichierExcel.java:88)
    	at fr.gui.execute.execute.main(execute.java:84)
    Une idée ? merci d'avance.

  2. #2
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2012
    Messages
    277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 277
    Points : 126
    Points
    126
    Par défaut Complément d'informations
    Voici ma classe qui sert à la création de l'excel à partir d'un fichier existant et que j'aimerais modifier par un ajout de Cellule en couleur selon résultat.

    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
    package fr.gui.metier;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Map;
     
    import jxl.Cell;
    import jxl.Workbook;
    import jxl.format.Colour;
    import jxl.format.UnderlineStyle;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
     
    public class CreationFichierExcel {
     
    	Workbook refWorkbook;
    	InputStream xlsRefStream;
    	WritableWorkbook outWorkbook;
     
    	/**
             * charge le fichier excel a modifier
             * 
             * @param fichierExcel
             * @return
             * @throws Exception
             */
    	public Workbook chargementModele(File fichierExcel) throws Exception {
    		try {
    			// Chargement du fichier "modèle"
    			this.xlsRefStream = new FileInputStream(fichierExcel);
    			this.refWorkbook = Workbook.getWorkbook(this.xlsRefStream);
    		} catch (Exception e) {
    			throw new Exception("fichier modèle introuvable ! ", e.getCause());
    		}
    		return this.refWorkbook;
    	}
     
    	/**
             * Crée du fichier de sortie
             * 
             * @param fichierExcelSortie
             * @param refWorkbook
             * @return
             * @throws Exception
             */
    	public WritableWorkbook creationFichierExcelSortie(File fichierExcelSortie, Workbook refWorkbook)
    			throws Exception {
    		try {
    			this.outWorkbook = Workbook.createWorkbook(fichierExcelSortie, refWorkbook);
    		} catch (IOException e) {
    			throw new Exception("création du fichier excel de sortie impossible", e.getCause());
    		}
    		return this.outWorkbook;
    	}
     
    	/**
             * Méthode de récupération du contenu de l'onglet Excel
             * 
             * @param numeroOnglet
             * @param outWorkbook
             * @param numeroColonneRef
             * @param rows
             * @param resultatProduitPresentDansLesDeuxExcels
             */
    	public void recupereOngletExcelCourant(Integer numeroOnglet, WritableWorkbook outWorkbook,
    			Integer numeroColonneRef, Map<String, String> resultatProduitPresentDansLesDeuxExcels) {
     
    		System.out.println("appel de la methode recuperer onglet Excel courant ! ");
    		// Récupération de l'onglet courant (le premier onglet)
    		WritableSheet out = outWorkbook.getSheet(numeroOnglet);
     
    		WritableCellFormat formatGreen = new WritableCellFormat(
    				new WritableFont(WritableFont.ARIAL, WritableFont.DEFAULT_POINT_SIZE, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, Colour.GREEN));
    		Label label = new Label(0, 0, "un texte en vert", formatGreen);
     
    		for (int i = 0; i < resultatProduitPresentDansLesDeuxExcels.size(); i++) {
    			Cell cellule = out.getCell(numeroColonneRef, i); // cols,
    																		// rows
    			if (resultatProduitPresentDansLesDeuxExcels.containsKey(cellule.getContents())) {
    				String celluleRecup = cellule.getContents();
    				System.out.println("cellule récupérée : " + celluleRecup);
    				out.setColumnView(numeroColonneRef, i, formatGreen);
     
    			}
    		}
     
    		System.out.println("fin du traitement ");
    		// Toutes les cellules sont remplies :
    		// Sauvegarde le fichier
    		try {
    			outWorkbook.write();
    			outWorkbook.close();
    		} catch (IOException | WriteException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    J'obtiens ça comme erreur qui démontre la ligne , et je ne comprends pas :

    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
    appel de la methode recuperer onglet Excel courant ! 
    fin du traitement 
    jxl.common.AssertionFailed
    	at jxl.common.Assert.verify(Assert.java:37)
    	at jxl.write.biff.CompoundFile.readAdditionalPropertySets(CompoundFile.java:398)
    	at jxl.write.biff.CompoundFile.<init>(CompoundFile.java:209)
    	at jxl.write.biff.File.close(File.java:116)
    	at jxl.write.biff.WritableWorkbookImpl.close(WritableWorkbookImpl.java:456)
    	at fr.gui.metier.CreationFichierExcel.recupereOngletExcelCourant(CreationFichierExcel.java:102)
    	at fr.gui.execute.execute.main(execute.java:84)
    Exception in thread "main" jxl.common.AssertionFailed
    	at jxl.common.Assert.verify(Assert.java:37)
    	at jxl.write.biff.CompoundFile.readAdditionalPropertySets(CompoundFile.java:398)
    	at jxl.write.biff.CompoundFile.<init>(CompoundFile.java:209)
    	at jxl.write.biff.File.close(File.java:116)
    	at jxl.write.biff.WritableWorkbookImpl.close(WritableWorkbookImpl.java:456)
    	at fr.gui.metier.CreationFichierExcel.recupereOngletExcelCourant(CreationFichierExcel.java:102)
    	at fr.gui.execute.execute.main(execute.java:84)

  3. #3
    Rédacteur/Modérateur
    Avatar de Logan Mauzaize
    Homme Profil pro
    Architecte technique
    Inscrit en
    Août 2005
    Messages
    2 894
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Transports

    Informations forums :
    Inscription : Août 2005
    Messages : 2 894
    Points : 7 083
    Points
    7 083
    Par défaut
    Si j'étais toi, je procéderai par étape :
    1. D'abord ouvrir et fermer le fichier
    2. Modifier la valeur du cellule
    3. Modifier la couleur de la cellule
    4. etc.


    A chaque étape, tu vérifies que ton code marche. Ca te permettra de cibler le code qui pose problèmes.
    Java : Cours et tutoriels - FAQ - Java SE 8 API - Programmation concurrente
    Ceylon : Installation - Concepts de base - Typage - Appels et arguments

    ECM = Exemple(reproduit le problème) Complet (code compilable) Minimal (ne postez pas votre application !)
    Une solution vous convient ? N'oubliez pas le tag
    Signature par pitipoisson

  4. #4
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2012
    Messages
    277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 277
    Points : 126
    Points
    126
    Par défaut
    Bon j'ai fait une classe la plus simple du monde afin d'essayer de trouver la raison de l'erreur hors je ne sais toujours pas, 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
    import java.io.File;
    import java.io.IOException;
     
    import jxl.Cell;
    import jxl.Workbook;
    import jxl.format.Colour;
    import jxl.format.UnderlineStyle;
    import jxl.read.biff.BiffException;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
     
    public class Test {
     
    	/**
             * @param args
             * @throws Exception
             */
    	static WritableWorkbook copy;
    	static WritableWorkbook out;
    	static WritableCellFormat formatGreen = new WritableCellFormat(new WritableFont(WritableFont.ARIAL,
    			WritableFont.DEFAULT_POINT_SIZE, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.GREEN));
     
    	public static void main(String[] args) throws Exception {
     
    		try {
     
    			// Chemin fichier de base
    			String filePath = "ExportInf.xls";
     
    			// Ouverture du fichier de base
    			Workbook workbook = Workbook.getWorkbook(new File(filePath));
     
    			// création d'un autre fichier xls de copie modifiable à partir de
    			// celui
    			// de base.
    			out = Workbook.createWorkbook(new File("ExportSortieInf.xls"), workbook);
     
    			// récupération du nombre de feuilles (Sheets)
    			System.out.println("nombre de Sheets : " + workbook.getNumberOfSheets());
     
    			// sélection de la première feuille n°0 en écriture
    			WritableSheet feuille = out.getSheet(0);
     
    			// format cellule trouvé dans les deux fichiers excel site et
    			// fournisseur.
    			for (int i = 0; i < feuille.getRows(); i++) {
    				Cell cellule = feuille.getCell(2, i);
    				System.out.println("contenu cellules " + cellule.getContents());
     
    				if (cellule.getContents().contains("Supplier Reference")) {
    					Label label = new Label(2, 0, cellule.getContents(), formatGreen);
    					feuille.addCell(label);
    				}
     
    			}
     
    			// écriture du fichier copie
    			out.write();
     
    			try {
    				// fermeture de la copie PLANTAGE SUR LA LIGNE OUT.CLOSE()
    				out.close();
     
    				// fermeture du fichier de base
    				workbook.close();
     
    			} catch (WriteException e1) {
    				e1.printStackTrace();
    			}
    		} catch (BiffException e2) {
    			e2.getStackTrace();
    		} catch (IOException e2) {
    			e2.printStackTrace();
    		}
     
    	}
     
    }
    Et voici ce que cela me donne en sortie :

    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
    nombre de Sheets : 1
    contenu cellules Supplier Reference
    jxl.common.AssertionFailed
    	at jxl.common.Assert.verify(Assert.java:37)
    	at jxl.write.biff.CompoundFile.readAdditionalPropertySets(CompoundFile.java:398)
    	at jxl.write.biff.CompoundFile.<init>(CompoundFile.java:209)
    	at jxl.write.biff.File.close(File.java:116)
    	at jxl.write.biff.WritableWorkbookImpl.close(WritableWorkbookImpl.java:456)
    	at Test.main(Test.java:67)
    Exception in thread "main" jxl.common.AssertionFailed
    	at jxl.common.Assert.verify(Assert.java:37)
    	at jxl.write.biff.CompoundFile.readAdditionalPropertySets(CompoundFile.java:398)
    	at jxl.write.biff.CompoundFile.<init>(CompoundFile.java:209)
    	at jxl.write.biff.File.close(File.java:116)
    	at jxl.write.biff.WritableWorkbookImpl.close(WritableWorkbookImpl.java:456)
    	at Test.main(Test.java:67)

  5. #5
    Rédacteur/Modérateur
    Avatar de Logan Mauzaize
    Homme Profil pro
    Architecte technique
    Inscrit en
    Août 2005
    Messages
    2 894
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Transports

    Informations forums :
    Inscription : Août 2005
    Messages : 2 894
    Points : 7 083
    Points
    7 083
    Par défaut
    J'ai testé avec la version 2.6.12 de JExcel et ce code marche parfaitement. Le problème vient soit du classeur, soit de la version de JExcel.


    Remarque : Tu contrôles la troisième colonne de chaque ligne mais c'est toujours la première colonne que tu reformates
    Java : Cours et tutoriels - FAQ - Java SE 8 API - Programmation concurrente
    Ceylon : Installation - Concepts de base - Typage - Appels et arguments

    ECM = Exemple(reproduit le problème) Complet (code compilable) Minimal (ne postez pas votre application !)
    Une solution vous convient ? N'oubliez pas le tag
    Signature par pitipoisson

  6. #6
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2012
    Messages
    277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2012
    Messages : 277
    Points : 126
    Points
    126
    Par défaut
    Effectivement j'étais tellement sûr que j'avais fait une bêtise dans le code que je n'ai pas vérifié au niveau du fichier excel, il y avait bien un soucis à la conception du fichier excel de base. Le programme fonctionne correctement une fois un copier coller de l'intégralité du contenu excel vers un autre fichier.

    Merci pour l'aide.

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

Discussions similaires

  1. Modèle MVC : JDBC + Java + Excel API
    Par D4rkArthemis dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 24/05/2011, 15h20
  2. Java et API windows xp
    Par yael20 dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 07/08/2006, 02h26
  3. [SOAP][JAVA] quelle api utiliser
    Par gandalf72 dans le forum XML/XSL et SOAP
    Réponses: 5
    Dernier message: 23/05/2006, 17h04
  4. Réponses: 4
    Dernier message: 09/11/2005, 17h11
  5. [JXL] utilisation jxl (java / excel)
    Par yoxx dans le forum Documents
    Réponses: 5
    Dernier message: 16/08/2005, 13h42

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