begin process at 2008 08 20 14:21:08
1 228 866 membres
232 nouveaux aujourd'hui
14 257 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

CLASSE JAVA POUR LA COMPARAISON DE 2 FICHIERS EXCEL


Information sur la source

Catégorie :Divers Classé sous : jexcelapi, excel, comparaison Niveau : Débutant Date de création : 05/10/2007 Date de mise à jour : 09/10/2007 06:58:14 Vu : 5 484

Note :
5 / 10 - par 1 personne
5,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

Description

Bonjour,

J'ai eu à traquer des évolutions minimes sur des fichiers excel volumineux contenant plusieurs feuilles. J'ai trouvé des tas de codes sources directement en VBA mais rien en java.

Voici une petite classe java utilisée pour comparer feuille à feuille 2 fichiers excel et écrire le résultat dans un fichier change.log.

Cette classe utilise jexcelapi (fichier jxl.jar) pour manipuler les fichiers excel (disponible sous http://jexcelapi.sourceforge.net/).

Merci a Twinuts pour son commentaire - j'ai débuté en Java il n'y a pas très longtemps...
J'ai mis à jour le code.

Source

  • package ihm_main;
  • import java.io.File;
  • import java.io.FileWriter;
  • import java.io.PrintWriter;
  • import jxl.Sheet;
  • import jxl.Workbook;
  • public class XLCompare {
  • private String dirname;
  • private static final String[] COLUMN = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
  • "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z", "AA", "AB", "AC",
  • "AD", "AE", "AF"};
  • /**
  • * Constructor
  • * @param dirname : directory where excel files are located
  • */
  • public XLCompare( String dirname) {
  • this.dirname = dirname;
  • }
  • /**
  • * This method compares reference and new excel file
  • */
  • public void fcompare( String fnew, String fref ) {
  • try {
  • // Open log file
  • PrintWriter plog = new PrintWriter( new FileWriter( this.dirname +
  • java.io.File.separator + "Change.log"));
  • // Open reference file
  • Workbook wref = Workbook.getWorkbook( new File( this.dirname +
  • java.io.File.separator + fref));
  • // Open new file
  • Workbook wnew = Workbook.getWorkbook( new File( this.dirname +
  • java.io.File.separator + fnew));
  • // Get sheet names in new file
  • String[] nsheet = wnew.getSheetNames();
  • // Check for all sheet from new file
  • for( int i=0; i<nsheet.length; i++) {
  • // Trace sheet name in log file
  • plog.println();
  • plog.println( "Sheet " + nsheet[i]);
  • // Open sheet in reference file
  • Sheet rsh = wref.getSheet( nsheet[i]);
  • if( rsh == null) {
  • plog.println( " new sheet. Not existing in reference file");
  • continue;
  • }
  • // Open sheet in new file
  • Sheet nsh = wnew.getSheet( nsheet[i]);
  • this.scompare( plog, nsh, rsh);
  • }
  • plog.close();
  • }
  • catch (Exception e) {
  • System.err.println( "Xl compare : " + e.toString());
  • }
  • }
  • /**
  • * This method compare 2 sheets
  • */
  • private void scompare( PrintWriter plog, Sheet nsh, Sheet rsh) {
  • int nmax = nsh.getRows();
  • for( int i=0; i<nmax; i++) {
  • for( int j=0; j<28; j++) {
  • String rlab = rsh.getCell( j, i).getContents();
  • String nlab = nsh.getCell( j, i).getContents();
  • if( !rlab.equals( nlab)) {
  • String texte = "line " + Integer.toString( i+1) + ", column " +
  • COLUMN[ j] + " : " + rlab + " -> " + nlab;
  • plog.println( " " + texte);
  • System.err.println( texte);
  • }
  • }
  • }
  • }
  • }
package ihm_main;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import jxl.Sheet;
import jxl.Workbook;

public class XLCompare {

	private	String		dirname;
	private	static final String[]	COLUMN = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
			"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z", "AA", "AB", "AC",
			"AD", "AE", "AF"};
	
	/**
	 * Constructor
	 * @param	dirname : directory where excel files are located
	 */
	public XLCompare( String dirname) {
		this.dirname = dirname;
	}
	
	/**
	 * This method compares reference and new excel file
	 */
	public void fcompare( String fnew, String fref ) {
 
		try {
			// Open log file
		   	PrintWriter plog = new PrintWriter( new FileWriter( this.dirname + 
		   			java.io.File.separator + "Change.log"));
		   	// Open reference file
			Workbook wref = Workbook.getWorkbook( new File( this.dirname + 
					java.io.File.separator + fref));
		   	// Open new file
			Workbook wnew = Workbook.getWorkbook( new File( this.dirname + 
					java.io.File.separator + fnew));
			// Get sheet names in new file
			String[] nsheet = wnew.getSheetNames();
			
			// Check for all sheet from new file
			for( int i=0; i<nsheet.length; i++) {
				// Trace sheet name in log file
				plog.println();
				plog.println( "Sheet " + nsheet[i]);
				// Open sheet in reference file
				Sheet rsh = wref.getSheet( nsheet[i]);
				if( rsh == null) {
					plog.println( "   new sheet. Not existing in reference file");
					continue;
				}
				// Open sheet in new file
				Sheet nsh = wnew.getSheet( nsheet[i]);
				this.scompare( plog, nsh, rsh);
			}		   	
			plog.close();
		}
		catch (Exception e) {
			System.err.println( "Xl compare : " + e.toString());
		}
	}

	/**
	 * This method compare 2 sheets
	 */
	private void scompare( PrintWriter plog, Sheet nsh, Sheet rsh) {
		
		int nmax = nsh.getRows();
		
		for( int i=0; i<nmax; i++) {
			for( int j=0; j<28; j++) {
				String rlab = rsh.getCell( j, i).getContents();
				String nlab = nsh.getCell( j, i).getContents();
				if( !rlab.equals( nlab)) {
					String texte = "line " + Integer.toString( i+1) + ", column " +
						COLUMN[ j] + " : " + rlab + " -> " + nlab;
					plog.println( "   " + texte);
					System.err.println( texte);
				}
			}
		}
	}
}

Conclusion

Seule restriction : je limitais la comparaison aux 28 premières colonnes pour mon application. A vous de voir dans votre contexte...
06 octobre 2007 14:42:51 :
Prise en compte des conseils de Twinuts - merci
06 octobre 2007 14:49:15 :
Voilà avec le bon code...
09 octobre 2007 06:58:14 :
Indication du lien vers le site JexcelAPI
  • signaler à un administrateur
    Commentaire de Twinuts le 05/10/2007 09:42:46 administrateur CS 5/10

    Salut,

    je n'ai pas testé et il m'est impossible de tester ton code dans l'état à cause de tes "\\".... et oui sans windoz ton code ne fonctionne pas sans un modification.... pour ta gouverne il existe java.io.File.separator qui te met \\ pour windoz et / pour linux et mac...

    Ensuite quelques petits détails :

    - En java une méthode est censé commencer par une minuscule et non une majuscule donc tes méthodes "Compare" devraient plus ressembler à "compare".

    - Pourquoi ne pas passer ton tableau 'column' en constante, vu que tu ne le modifies pas ?
    private static final String[] COLUMN = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
             "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z", "AA", "AB", "AC",
             "AD", "AE", "AF"};

    - Pourquoi linker ton code avec SWT ? perso je ne vois pas l'intérêt vu qu'il se rapproche plus d'une petite classe API qui n'as pas besoin d'IHM.

  • signaler à un administrateur
    Commentaire de farid_kidari le 07/10/2007 10:22:29

    Bonjour,

    Où trouver l'API jxl ?

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS