Création d'un fichier EXCEL - utilisation de JXL : Java Excel API

Voici un exemple de code permettant de créer un fichier EXCEL grâce à l’utilisation de l’API JXL : Télécharger Java Excel API
package xls;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.ScriptStyle;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class EcrireEXCEL {
public static void main(String[] args) {
try {
WritableWorkbook workbook = Workbook.createWorkbook(new File("sortie.xls"));
WritableSheet sheet = workbook.createSheet("Premier classeur", 0);
//Crée le format d’une cellule
WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD, true, UnderlineStyle.NO_UNDERLINE,Colour.BLUE, ScriptStyle.NORMAL_SCRIPT);
WritableCellFormat arial10format = new WritableCellFormat(arial10font);
//Crée un label à la ligne 0, colonne 0 avec le format spécifique
Label label = new Label(0, 0, "Arial 10 point label",arial10format);
//Crée un label à la ligne 2, colonne 0 sans style prédéfini
Label label2 = new Label(0, 2, "Résultat");
//Ajout des cellules
sheet.addCell(label);
sheet.addCell(label2);
//Ajout d’une cellule ligne 2, colonne 1
Number number = new Number(1, 2, 3.1459);
sheet.addCell(number);
//Ajout d’une image ligne 4, colonne 0
//Taille de l’image : 6 lignes et 2 colonnes
WritableImage image = new WritableImage(0, 4, 2, 6,new File("Logo-Labo-Sun.png"));
sheet.addImage(image);
//Ecriture et fermeture du classeur
workbook.write();
workbook.close();
} catch (RowsExceededException e1) {
e1.printStackTrace();
} catch (WriteException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
System.out.println("Le fichier \"sortie.xls\" à été généré correctement.");
}
}
}

Laboratoire SUPINFO des technologies Sun
labo-sun@supinfo.com


Conditions d'utilisation et © Copyright SUPINFO International University
23, rue de Château Landon - 75010 PARIS - Tél : +33 (0) 153359700 Fax : +33 (0) 153359701
Respect de la vie privée