Package practica1.manejoExperimento

Source Code of practica1.manejoExperimento.ExportarExperimentoExcel

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package practica1.manejoExperimento;

import es.miguelgonzalez.jgraficacomida.JGraficaComidaDibujo;
import es.miguelgonzalez.jgraficacomida.ModeloGraficaComida;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.*;
import practica1.Practica1;
import practica1.language.Language;
import practica1.logic.LogicExperimento;
import practica1.logic.LogicPoblacion;
import practica1.ui.tablaPoblacion.ModeloPoblacionTabla;

/**
* Clase que permite exportar un experimento a excel
* @author Miguel González - Ceura
*/
public class ExportarExperimentoExcel {
    /**
     * Método estático que exporta el experimento a excel
     * @param experimento LogicExperimento a exportar
     * @throws ExperimentoInvalidoException Lanza un error si no se pudo exportar
     */
    public static void ExportarExcel(LogicExperimento experimento)
            throws ExperimentoInvalidoException {
        try {
            OutputStream output = new FileOutputStream(experimento.
                    getFichExperimento().getParent() + "/" +
                    experimento.getNombreExperimento() + ".xls", false);
            WritableWorkbook workbook = Workbook.createWorkbook(output);
           
            ArrayList<LogicPoblacion> poblaciones =
                    experimento.getPoblaciones();
           
            //Estilo centrado y con subrayado
            WritableCellFormat wcf1 = new WritableCellFormat();
            wcf1.setAlignment(Alignment.CENTRE);
            wcf1.setBorder(jxl.format.Border.BOTTOM,
                    jxl.format.BorderLineStyle.MEDIUM_DASHED,
                    jxl.format.Colour.GRAY_80);
           
            //Estilo centrado y con bordes
            WritableCellFormat wcf2 = new WritableCellFormat();
            wcf2.setAlignment(Alignment.CENTRE);
            wcf2.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.DOTTED,
                    jxl.format.Colour.GRAY_80);
           
            //Primera página que contiene los datos del experimento
            WritableSheet sheetP = workbook.createSheet(
                        experimento.getNombreExperimento(), 1);
           
            sheetP.setColumnView(1, 25); //Establecemos el ancho de la columna 1
            sheetP.setColumnView(2, 20);
           
            //new Label(Columna, Fila, Texto)
            sheetP.addCell(new Label(1, 2,
                    Language.getI().getP("NOMBRE_EXPERIMENTO")));
            sheetP.addCell(new Label(2, 2, experimento.getNombreExperimento()));
           
            sheetP.addCell(new Label(1, 3,
                    Language.getI().getP("TIPO_BACTERIA")));
            sheetP.addCell(new Label(2, 3, experimento.getTipoBacteria()));
           
            sheetP.addCell(new Label(1, 4,
                    Language.getI().getP("NOMBRE_INVESTIGADOR")));
            sheetP.addCell(new Label(2, 4, experimento.getNombreInvestigador()));
           
            sheetP.addCell(new Label(1, 5,
                    Language.getI().getP("PROYECTO_INVESTIGACION")));
            sheetP.addCell(new Label(2, 5, experimento.getProyectoInvestigacion()));
           
            //Empezamos las poblaciones en la segunda página
            int contadorPagina = 2;
            for(LogicPoblacion poblacion : poblaciones) {
                WritableSheet sheet = workbook.createSheet(
                        poblacion.getNombrePoblacion(), contadorPagina);
               
                //Establecemos los anchos de las columnas
                sheet.setColumnView(1, 18);
                sheet.setColumnView(2, 18);
                sheet.setColumnView(3, 18);
                sheet.setColumnView(4, 18);
                sheet.setColumnView(5, 18);
               
                //Datos generales de la población
                //Columna , Fila, Dato
                sheet.mergeCells(2, 2, 4, 2);
                sheet.addCell(new Label(2, 2, poblacion.getNombrePoblacion(), wcf1));
               
                sheet.addCell(new Label(1, 4,
                        Language.getI().getP("FECHA") + ": "));
                sheet.addCell(new Label(2, 4, poblacion.getFecha()));
               
                sheet.addCell(new Label(1, 5,
                        Language.getI().getP("TAMANIO") + ": "));
                sheet.addCell(new Label(2, 5, ""+poblacion.getTamanioPoblacion()));
               
                sheet.addCell(new Label(1, 6,
                        Language.getI().getP("LUMINOSIDAD") + ": "));
                sheet.addCell(new Label(2, 6, poblacion.getLuminosidad()));
               
                sheet.addCell(new Label(1, 7,
                        Language.getI().getP("TEMPERATURA") + ": "));
                sheet.addCell(new Label(2, 7, poblacion.getTemperatura() +
                        poblacion.getEscalaTemperatura()));
               
                //Modelo de la gráfica
                sheet.mergeCells(2, 9, 4, 9);
                sheet.addCell(new Label(2, 9,
                        Language.getI().getP("ALIMENTACION"), wcf1));
                ModeloGraficaComida grafica = poblacion.getModeloGraficaComida();
               
                sheet.addCell(new Label(1, 12,
                        Language.getI().getP("ALIMENTACION_INICIAL") + ": "));
                sheet.addCell(new Label(2, 12, ""+grafica.getAlimentoInicial()));
               
                sheet.addCell(new Label(1, 13,
                        Language.getI().getP("ALIMENTACION_MAXIMA") + ": "));
                sheet.addCell(new Label(2, 13, ""+grafica.getAlimentoMax()));
               
                sheet.addCell(new Label(1, 14,
                        Language.getI().getP("DIA_MAXIMO") + ": "));
                sheet.addCell(new Label(2, 14, ""+grafica.getDiaMax()));
               
                sheet.addCell(new Label(1, 15,
                        Language.getI().getP("ALIMENTACION_FINAL") + ": "));
                sheet.addCell(new Label(2, 15, ""+grafica.getAlimentoMax()));
               
                //Gráfica de la población
               
                BufferedImage imgGrafica = new BufferedImage(300, 200,
                    BufferedImage.TYPE_INT_RGB);
                JGraficaComidaDibujo jGrafica = new JGraficaComidaDibujo(grafica);
                jGrafica.setSize(300,200);
               
                Graphics g = imgGrafica.getGraphics();
                jGrafica.paint(g);
               
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write( imgGrafica, "jpg", baos );
                baos.flush();
                byte[] imageInByte = baos.toByteArray();
                baos.close();
               
                WritableImage wi = new WritableImage
                    (3, 11, 3, 7, imageInByte);
                sheet.addImage(wi);
               
                //Tabla
                sheet.mergeCells(2, 18, 4, 18);
                sheet.addCell(new Label(2, 18,
                        Language.getI().getP("TABLA"), wcf1));
                ModeloPoblacionTabla tabla = poblacion.
                        getModeloPoblacionTabla();
               
                //Cabecera de la tabla
                sheet.addCell(new Label(1, 20,
                        Language.getI().getP("DIA"), wcf2));
                sheet.addCell(new Label(2, 20,
                        Language.getI().getP("TAMANIO"), wcf2));
                sheet.addCell(new Label(3, 20,
                        Language.getI().getP("BACTERIAS_MUERTAS"), wcf2));
                sheet.addCell(new Label(4, 20,
                        Language.getI().getP("TEMPERATURA"), wcf2));
                sheet.addCell(new Label(5, 20,
                        Language.getI().getP("DOSIS_COMIDA"), wcf2));
               
                //Datos de la tabla
                for(int i=0; i<30; i++) {
                    sheet.addCell(new Label(1, 21 + i,
                            (String)tabla.getValueAt(i, 0), wcf2));
                    sheet.addCell(new Label(2, 21 + i,
                            (String)tabla.getValueAt(i, 1), wcf2));
                    sheet.addCell(new Label(3, 21 + i,
                            (String)tabla.getValueAt(i, 2), wcf2));
                    sheet.addCell(new Label(4, 21 + i,
                            (String)tabla.getValueAt(i, 3), wcf2));
                    sheet.addCell(new Label(5, 21 + i,
                            (String)tabla.getValueAt(i, 4), wcf2));
                }
               
                contadorPagina++;
            }
           
            workbook.write();
            workbook.close();
        } catch (WriteException ex) {
            Practica1.log.error(Language.getI().getP("ERROR"), ex);
            throw new ExperimentoInvalidoException(ex);
        } catch(IOException ex) {
            Practica1.log.error(Language.getI().getP("ERROR"), ex);
            throw new ExperimentoInvalidoException(ex);
        }
    }
}
TOP

Related Classes of practica1.manejoExperimento.ExportarExperimentoExcel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.