Java tutorial
package nc.noumea.mairie.organigramme.utils; /* * #%L * Logiciel de Gestion des Organigrammes de la Ville de Nouma * $Id:$ * $HeadURL:$ * %% * Copyright (C) 2015 Mairie de Nouma * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import nc.noumea.mairie.organigramme.core.utility.DateUtil; import nc.noumea.mairie.organigramme.core.ws.ISirhWSConsumer; import nc.noumea.mairie.organigramme.dto.EntiteDto; import nc.noumea.mairie.organigramme.dto.FichePosteDto; import nc.noumea.mairie.organigramme.enums.StatutFichePoste; import org.apache.commons.io.IOUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.zkoss.bind.annotation.GlobalCommand; import org.zkoss.zul.Filedownload; public class ExportExcelUtil { private static final int NUM_COL_SIGLE = 0; private static final int NUM_COL_NUMERO = 1; private static final int NUM_COL_TITRE = 2; private static final int NUM_COL_STATUT = 3; private static final int NUM_COL_CATEGORIE = 4; private static final int NUM_COL_AGENT = 5; private static final int NUM_COL_REGLEMENTAIRE = 6; private static final int NUM_COL_COMMENTAIRE = 7; @GlobalCommand public static void genereExportExcel(EntiteDto entiteDto, boolean afficheFdpInactive, ISirhWSConsumer sirhWSConsumer) throws IOException { Workbook wb = new HSSFWorkbook(); Sheet feuillePrincipale = wb.createSheet("Fiches de postes"); creerEntete(feuillePrincipale); String listIdStatutFDP = StatutFichePoste.getListIdStatutActif(); if (afficheFdpInactive) { listIdStatutFDP += "," + StatutFichePoste.INACTIVE.getId(); } int numeroLigne = 1; for (FichePosteDto fichePosteDto : sirhWSConsumer.getFichePosteByIdEntite(entiteDto.getIdEntite(), listIdStatutFDP, true)) { Row row = feuillePrincipale.createRow((short) numeroLigne); row.createCell(NUM_COL_SIGLE).setCellValue(fichePosteDto.getSigle()); row.createCell(NUM_COL_NUMERO).setCellValue(fichePosteDto.getNumero()); row.createCell(NUM_COL_TITRE).setCellValue(fichePosteDto.getTitre()); row.createCell(NUM_COL_STATUT).setCellValue(fichePosteDto.getStatutFDP()); row.createCell(NUM_COL_CATEGORIE).setCellValue(fichePosteDto.getLibelleGradeCategorie()); row.createCell(NUM_COL_AGENT).setCellValue(fichePosteDto.getAgent()); row.createCell(NUM_COL_REGLEMENTAIRE).setCellValue(fichePosteDto.getReglementaire()); row.createCell(NUM_COL_COMMENTAIRE).setCellValue(fichePosteDto.getCommentaire()); numeroLigne++; } // Aucolumns for (int i = NUM_COL_SIGLE; i <= NUM_COL_COMMENTAIRE; i++) { feuillePrincipale.autoSizeColumn(i); } File tempFile = File.createTempFile("tempFile", ".xls"); FileOutputStream fileStream = new FileOutputStream(tempFile); wb.write(fileStream); FileInputStream fileInputStream = new FileInputStream(tempFile); // Cration et sauvegarde du fichier String nomFichier = "Organigramme - Impression Fiche Poste - " + entiteDto.getSigle() + "-" + DateUtil.formatDateForFile(new Date()) + ".xls"; Filedownload.save(IOUtils.toByteArray(fileInputStream), null, nomFichier); } private static void creerEntete(Sheet sheet) { Row rowEntete = sheet.createRow((short) 0); rowEntete.createCell(NUM_COL_SIGLE).setCellValue("Sigle"); rowEntete.createCell(NUM_COL_NUMERO).setCellValue("Numro"); rowEntete.createCell(NUM_COL_TITRE).setCellValue("Titre"); rowEntete.createCell(NUM_COL_STATUT).setCellValue("Statut"); rowEntete.createCell(NUM_COL_CATEGORIE).setCellValue("Catgorie"); rowEntete.createCell(NUM_COL_AGENT).setCellValue("Agent"); rowEntete.createCell(NUM_COL_REGLEMENTAIRE).setCellValue("Rglementaire"); rowEntete.createCell(NUM_COL_COMMENTAIRE).setCellValue("Commentaire"); } }