|
begin process at 2008 08 20 14:21:08
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
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
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...
Historique
- 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
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | | | | 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 |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|