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();
		}
	}
} | 
Partager