edu.aplic.utilerias.GenerarArchivoXlsUtil.java Source code

Java tutorial

Introduction

Here is the source code for edu.aplic.utilerias.GenerarArchivoXlsUtil.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.aplic.utilerias;

import edu.aplic.datos.Compra;
import edu.aplic.datos.Gasto;
import edu.aplic.datos.ProductoComprado;
import edu.aplic.datos.ProductoVendido;
import edu.aplic.datos.Usuario;
import edu.aplic.datos.Venta;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 *
 * @author rogelioesl
 */
public class GenerarArchivoXlsUtil {

    private static DecimalFormat formato;
    private static Utilerias oUtil;

    static {
        DecimalFormat numFloat = new DecimalFormat("0.00");
        DecimalFormatSymbols dfs = numFloat.getDecimalFormatSymbols();
        dfs.setDecimalSeparator('.');
        formato = new DecimalFormat("0.00", dfs);

        oUtil = new Utilerias();
    }

    public static void crearXslVentas(File file, Venta[] aVentas, Usuario oUsuario) throws IOException {
        FileOutputStream elFichero = null;
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();
        HSSFRow fila = null;
        HSSFCell celda = null;

        fila = hoja.createRow(0);
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        celda.setCellValue(new HSSFRichTextString("Reporte Ventas"));

        if (oUsuario != null) {
            celda = fila.createCell(1);
            hoja.autoSizeColumn(0);
            celda.setCellValue(new HSSFRichTextString("Usuario: " + oUsuario.getNombreCompleto()));
        }

        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(1);
        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Fecha"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Total"));

        ProductoVendido oProductoVendido = new ProductoVendido();
        ProductoVendido[] aProductoVendido = null;
        double nTotal = 0.0D;
        int rows = 1;
        if (aVentas != null && aVentas.length > 0) {

            for (Venta oVentaTemp : aVentas) {
                ++rows;
                hoja.autoSizeColumn(0);
                fila = hoja.createRow(rows);
                //                Usuario oUsuario = new Usuario();
                //                oUsuario = oVentaTemp.getUsuario();    
                aProductoVendido = oVentaTemp.getProductoVendido();

                // Se crea una celda dentro de la fila
                celda = fila.createCell(0);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(oUtil.ordenarDiaMesAnio(oVentaTemp.getFecha())));
                // Se crea una celda dentro de la fila
                celda = fila.createCell(1);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(formato.format(aProductoVendido[0].getImporte())));
                nTotal += aProductoVendido[0].getImporte();
            }

            ++rows;
            fila = hoja.createRow(rows);
            // Se crea una celda dentro de la fila
            celda = fila.createCell(0);
            // Se crea el contenido de la celda y se mete en ella.
            //HSSFRichTextString texton = new HSSFRichTextString("value");
            celda.setCellValue(new HSSFRichTextString("Total"));
            // Se crea una celda dentro de la fila
            celda = fila.createCell(1);
            // Se crea el contenido de la celda y se mete en ella.
            celda.setCellValue(new HSSFRichTextString(formato.format(nTotal)));
            //this.campoTotal1.setText(formato.format(nTotal));           
        }

        // Se salva el libro.
        try {
            //FileOutputStream elFichero = new FileOutputStream("ventas"+new Date().toString()+".xls");
            elFichero = new FileOutputStream(file);

            libro.write(elFichero);
            JOptionPane.showMessageDialog(null, "Reporte de ventas generado correctamente \n " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            elFichero.close();
        }
    }

    public static void crearXslVentasPorTipoProducto(File file, Venta[] aVentas, Usuario oUsuario)
            throws IOException {
        FileOutputStream elFichero = null;
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();
        HSSFRow fila = null;
        HSSFCell celda = null;

        fila = hoja.createRow(0);
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        celda.setCellValue(new HSSFRichTextString("Reporte Ventas por tipos"));

        if (oUsuario != null) {
            celda = fila.createCell(1);
            hoja.autoSizeColumn(0);
            celda.setCellValue(new HSSFRichTextString("Usuario: " + oUsuario.getNombreCompleto()));
        }

        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(1);
        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Fecha"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Total"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(2);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Tipo"));

        // Style Cell 2B
        HSSFCellStyle cellStyle = libro.createCellStyle();
        cellStyle = libro.createCellStyle();
        cellStyle.setFillForegroundColor(org.apache.poi.hssf.util.HSSFColor.GREY_25_PERCENT.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setBorderTop((short) 1); // single line border
        cellStyle.setBorderBottom((short) 1); // single line border

        ProductoVendido oProductoVendido = new ProductoVendido();
        ProductoVendido[] aProductoVendido = null;
        double nTotal = 0.0D;
        int rows = 1;
        String sFechaTemp = null;
        boolean bColor = true;
        if (aVentas != null && aVentas.length > 0) {
            boolean bPrimerFecha = true;
            for (Venta oVentaTemp : aVentas) {

                // Contador para inicializar la fecha del primer elemento
                if (bPrimerFecha) {
                    sFechaTemp = oUtil.ordenarDiaMesAnio(oVentaTemp.getFecha());
                    bPrimerFecha = false;
                } else {
                    String sFechaFila = oUtil.ordenarDiaMesAnio(oVentaTemp.getFecha());

                    if (!sFechaTemp.equals(sFechaFila)) {
                        bColor = !bColor;
                    }

                    if (bColor) {
                        cellStyle = libro.createCellStyle();
                        cellStyle.setFillForegroundColor(org.apache.poi.hssf.util.HSSFColor.GREY_25_PERCENT.index);
                        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                        cellStyle.setBorderTop((short) 1); // single line border
                        cellStyle.setBorderBottom((short) 1); // single line border

                    } else {
                        cellStyle = libro.createCellStyle();
                        cellStyle.setFillForegroundColor(org.apache.poi.hssf.util.HSSFColor.BLUE_GREY.index);
                        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                        cellStyle.setBorderTop((short) 1); // single line border
                        cellStyle.setBorderBottom((short) 1); // single line border

                    }

                    sFechaTemp = sFechaFila;

                }

                ++rows;
                hoja.autoSizeColumn(0);
                fila = hoja.createRow(rows);
                //                Usuario oUsuario = new Usuario();
                //                oUsuario = oVentaTemp.getUsuario();    
                aProductoVendido = oVentaTemp.getProductoVendido();

                // Se crea una celda dentro de la fila
                celda = fila.createCell(0);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(oUtil.ordenarDiaMesAnio(oVentaTemp.getFecha())));
                celda.setCellStyle(cellStyle);

                // Se crea una celda dentro de la fila
                celda = fila.createCell(1);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(formato.format(aProductoVendido[0].getImporte())));
                celda.setCellStyle(cellStyle);

                // Se crea una celda dentro de la fila
                celda = fila.createCell(2);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(aProductoVendido[0].getProducto().getTipo()));
                celda.setCellStyle(cellStyle);

                nTotal += aProductoVendido[0].getImporte();
            }

            ++rows;
            fila = hoja.createRow(rows);
            // Se crea una celda dentro de la fila
            celda = fila.createCell(0);
            // Se crea el contenido de la celda y se mete en ella.
            //HSSFRichTextString texton = new HSSFRichTextString("value");
            celda.setCellValue(new HSSFRichTextString("Total"));
            // Se crea una celda dentro de la fila
            celda = fila.createCell(1);
            // Se crea el contenido de la celda y se mete en ella.
            celda.setCellValue(new HSSFRichTextString(formato.format(nTotal)));
            //this.campoTotal1.setText(formato.format(nTotal));           
        }

        // Se salva el libro.
        try {
            //FileOutputStream elFichero = new FileOutputStream("ventas"+new Date().toString()+".xls");
            elFichero = new FileOutputStream(file);

            libro.write(elFichero);
            JOptionPane.showMessageDialog(null, "Reporte de ventas generado correctamente \n " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            elFichero.close();
        }
    }

    public static void crearXslConsumos(File file, Venta[] aVentas, Usuario oUsuario) throws IOException {
        FileOutputStream elFichero = null;
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();
        HSSFRow fila = null;
        HSSFCell celda = null;

        fila = hoja.createRow(0);
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        celda.setCellValue(new HSSFRichTextString("Reporte Consumos"));

        if (oUsuario != null) {
            celda = fila.createCell(1);
            hoja.autoSizeColumn(0);
            celda.setCellValue(new HSSFRichTextString("Usuario: " + oUsuario.getNombreCompleto()));
        }

        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(1);
        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Fecha"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Total"));

        ProductoVendido oProductoVendido = new ProductoVendido();
        ProductoVendido[] aProductoVendido = null;
        double nTotal = 0.0D;
        int rows = 1;
        if (aVentas != null && aVentas.length > 0) {

            for (Venta oVentaTemp : aVentas) {
                ++rows;
                hoja.autoSizeColumn(0);
                fila = hoja.createRow(rows);
                //                Usuario oUsuario = new Usuario();
                //                oUsuario = oVentaTemp.getUsuario();    
                aProductoVendido = oVentaTemp.getProductoVendido();

                // Se crea una celda dentro de la fila
                celda = fila.createCell(0);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(oUtil.ordenarDiaMesAnio(oVentaTemp.getFecha())));
                // Se crea una celda dentro de la fila
                celda = fila.createCell(1);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(formato.format(aProductoVendido[0].getImporte())));
                nTotal += aProductoVendido[0].getImporte();
            }

            ++rows;
            fila = hoja.createRow(rows);
            // Se crea una celda dentro de la fila
            celda = fila.createCell(0);
            // Se crea el contenido de la celda y se mete en ella.
            //HSSFRichTextString texton = new HSSFRichTextString("value");
            celda.setCellValue(new HSSFRichTextString("Total"));
            // Se crea una celda dentro de la fila
            celda = fila.createCell(1);
            // Se crea el contenido de la celda y se mete en ella.
            celda.setCellValue(new HSSFRichTextString(formato.format(nTotal)));
            //this.campoTotal1.setText(formato.format(nTotal));           
        }

        // Se salva el libro.
        try {
            //FileOutputStream elFichero = new FileOutputStream("ventas"+new Date().toString()+".xls");
            elFichero = new FileOutputStream(file);

            libro.write(elFichero);
            JOptionPane.showMessageDialog(null, "Reporte de consumos generado correctamente \n " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            elFichero.close();
        }
    }

    public static void crearXlsCompras(File file, Compra[] aCompras, Usuario oUsuario) throws IOException {

        FileOutputStream elFichero = null;
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();
        HSSFRow fila = null;
        HSSFCell celda = null;

        fila = hoja.createRow(0);
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        celda.setCellValue(new HSSFRichTextString("Reporte Compras"));

        if (oUsuario != null) {
            celda = fila.createCell(1);
            hoja.autoSizeColumn(0);
            celda.setCellValue(new HSSFRichTextString("Usuario: " + oUsuario.getNombreCompleto()));
        }

        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(1);
        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Fecha"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Total"));

        ProductoComprado oProductoComprado = new ProductoComprado();
        ProductoComprado[] aProductoComprado = null;

        double nTotal3 = 0.0D;
        int rows = 1;
        if (aCompras != null && aCompras.length > 0) {
            for (Compra oCompraTemp : aCompras) {
                ++rows;
                hoja.autoSizeColumn(0);
                fila = hoja.createRow(rows);

                //                Usuario oUsuario = new Usuario();
                //                oUsuario = oCompraTemp.getUsuario();    
                aProductoComprado = oCompraTemp.getProductoComprado();

                // Se crea una celda dentro de la fila
                celda = fila.createCell(0);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(oUtil.ordenarDiaMesAnio(oCompraTemp.getFecha())));
                // Se crea una celda dentro de la fila
                celda = fila.createCell(1);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(formato.format(aProductoComprado[0].getMonto())));
                nTotal3 += aProductoComprado[0].getMonto();
            }
            ++rows;
            fila = hoja.createRow(rows);
            // Se crea una celda dentro de la fila
            celda = fila.createCell(0);
            // Se crea el contenido de la celda y se mete en ella.
            //HSSFRichTextString texton = new HSSFRichTextString("value");
            celda.setCellValue(new HSSFRichTextString("Total"));
            // Se crea una celda dentro de la fila
            celda = fila.createCell(1);
            // Se crea el contenido de la celda y se mete en ella.
            celda.setCellValue(new HSSFRichTextString(formato.format(nTotal3)));
            //this.campoTotal1.setText(formato.format(nTotal));   
        }
        // Se salva el libro.
        try {
            //FileOutputStream elFichero = new FileOutputStream("ventas"+new Date().toString()+".xls");
            elFichero = new FileOutputStream(file);

            libro.write(elFichero);
            JOptionPane.showMessageDialog(null, "Reporte de compras generado correctamente \n " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            elFichero.close();
        }
    }

    public static void crearXlsGastos(File file, Gasto[] aGastos, Usuario oUsuario) throws IOException {

        FileOutputStream elFichero = null;
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();
        HSSFRow fila = null;
        HSSFCell celda = null;

        fila = hoja.createRow(0);
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        celda.setCellValue(new HSSFRichTextString("Reporte Gastos"));

        if (oUsuario != null) {
            celda = fila.createCell(1);
            hoja.autoSizeColumn(0);
            celda.setCellValue(new HSSFRichTextString("Usuario: " + oUsuario.getNombreCompleto()));
        }

        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(1);
        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);
        hoja.autoSizeColumn(0);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Fecha"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Total"));

        double nTotal = 0.0D;
        int rows = 1;
        if (aGastos != null && aGastos.length > 0) {
            for (Gasto oGastoTemp : aGastos) {
                ++rows;
                hoja.autoSizeColumn(0);
                fila = hoja.createRow(rows);

                //                Usuario oUsuario = new Usuario();
                //                oUsuario = oGastoTemp.getUsuario();    

                // Se crea una celda dentro de la fila
                celda = fila.createCell(0);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(oUtil.ordenarDiaMesAnio(oGastoTemp.getFecha())));
                // Se crea una celda dentro de la fila
                celda = fila.createCell(1);
                // Se crea el contenido de la celda y se mete en ella.
                celda.setCellValue(new HSSFRichTextString(formato.format(oGastoTemp.getMonto())));
                nTotal += oGastoTemp.getMonto();
            }
            ++rows;
            fila = hoja.createRow(rows);
            // Se crea una celda dentro de la fila
            celda = fila.createCell(0);
            // Se crea el contenido de la celda y se mete en ella.
            //HSSFRichTextString texton = new HSSFRichTextString("value");
            celda.setCellValue(new HSSFRichTextString("Total"));
            // Se crea una celda dentro de la fila
            celda = fila.createCell(1);
            // Se crea el contenido de la celda y se mete en ella.
            celda.setCellValue(new HSSFRichTextString(formato.format(nTotal)));
            //this.campoTotal1.setText(formato.format(nTotal));   
        }
        // Se salva el libro.
        try {
            //FileOutputStream elFichero = new FileOutputStream("ventas"+new Date().toString()+".xls");
            elFichero = new FileOutputStream(file);

            libro.write(elFichero);
            JOptionPane.showMessageDialog(null, "Reporte de gastos generado correctamente \n " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            elFichero.close();
        }
    }
}