Après les tests pour générer du pdf, MonDossierWeb doit générer des fichier excel, nous avons testé POI. Apres quelques essais nous avons réussi à obtenir le résultat voulu grâce à la librairie POI. Voici comment sont implantées les choses :
Tout d’abord il faut penser à importer la librairie poi-2.5.1-final-20040804.jar disponible ici dans le projet.
Puis, dans notre Controller qui contient (en attribut : nne, nom, dossier, annee etc.) les informations affichées à l’écran, nous ajoutons la méthode exportExcel():
public String exportExcel(){
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext external = context.getExternalContext();
HttpServletResponse response = (HttpServletResponse) external.getResponse();
//formatage de la réponse
response.setContentType(“application/octet-stream”);
response.setHeader (“Content-Disposition”, “attachment; filename=\”test.xls\”" );
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
//creation du fichier excel
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(“etat-civil” );
//formatage taille de la premiere colonne
sheet.setColumnWidth((short)0,(short)(20*256));
// Creation des colonnes
HSSFRow row = sheet.createRow((short)0);
HSSFRow row1 = sheet.createRow((short)1);
HSSFRow row2 = sheet.createRow((short)2);
HSSFRow row3 = sheet.createRow((short)3);
HSSFRow row4 = sheet.createRow((short)4);
HSSFRow row5 = sheet.createRow((short)5);
HSSFRow row6 = sheet.createRow((short)6);
HSSFRow row7 = sheet.createRow((short)7);
//CREATION DES STYLES:
//STYLE1:
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillBackgroundColor((short) HSSFColor.DARK_RED.index);
headerStyle.setFillForegroundColor(HSSFColor.DARK_RED.index);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.WHITE.index);
font.setBoldweight((short) 10);
headerStyle.setFont(font);
//bordure style1
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setBottomBorderColor(HSSFColor.BLACK.index);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setLeftBorderColor(HSSFColor.BLACK.index);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setRightBorderColor(HSSFColor.BLACK.index);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setTopBorderColor(HSSFColor.BLACK.index);
//STYLE2:
HSSFCellStyle headerStyle2 = wb.createCellStyle();
headerStyle2.setFillBackgroundColor((short) HSSFColor.BLUE.index);
headerStyle2.setFillForegroundColor(HSSFColor.BLUE.index);
headerStyle2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle2.setFont(font);
//bordure style2
headerStyle2.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
headerStyle2.setBottomBorderColor(HSSFColor.BLACK.index);
headerStyle2.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
headerStyle2.setLeftBorderColor(HSSFColor.BLACK.index);
headerStyle2.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
headerStyle2.setRightBorderColor(HSSFColor.BLACK.index);
headerStyle2.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
headerStyle2.setTopBorderColor(HSSFColor.BLACK.index);
//Creation des cellules
HSSFCell cellLib = row.createCell((short) 0);
cellLib.setCellStyle(headerStyle);
cellLib.setCellValue(“ETAT-CIVIL” );
HSSFCell cellLib2 = row1.createCell((short) 0);
cellLib2.setCellStyle(headerStyle2);
cellLib2.setCellValue(“GENERALITES” );
row2.createCell((short)0).setCellValue(“Dossier “);
row2.createCell((short)1).setCellValue(dossier);
row3.createCell((short)0).setCellValue(“NNE “);
row3.createCell((short)1).setCellValue(nne);
row4.createCell((short)0).setCellValue(“Nom “);
row4.createCell((short)1).setCellValue(nom);
HSSFCell cellLib3 = row5.createCell((short) 0);
cellLib3.setCellStyle(headerStyle2);
cellLib3.setCellValue(“INSCRIPTION UNIVERSITAIRE” );
row6.createCell((short)0).setCellValue(“Anee “);
row6.createCell((short)1).setCellValue(annee);
row7.createCell((short)0).setCellValue(“Etablissement “);
row7.createCell((short)1).setCellValue(etablissement);
//FUSION des CELLULES:
Region reg=new Region(0,(short)0,0,(short)2);
Region reg2=new Region(1,(short)0,1,(short)2);
Region reg3=new Region(5,(short)0,5,(short)2);
sheet.addMergedRegion(reg);
sheet.addMergedRegion(reg2);
sheet.addMergedRegion(reg3);
// Ecriture dans l’output
ServletOutputStream out;
try {
out = response.getOutputStream();
wb.write(baos);
baos.writeTo(out);
baos.flush();
context.responseComplete();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Enfin, il ne reste plus qu’à ajouter un lien dans notre page xhtml, qui va nous proposer d’enregistrer le pdf généré:
<h:commandLink id=”link” action=”#{controller.exportExcel}”>
EXCEL
</h:commandLink>
Ainsi, en cliquant sur le lien ‘EXCEL’ vous aurez l’invitation suivante :
Il ne reste plus qu’à enregistrer le fichier. Voir le resultat
juin 25, 2007 à 6:00
lone star ford houston