Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook createCellStyle

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook createCellStyle

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook createCellStyle.

Prototype

@Override
public HSSFCellStyle createCellStyle() 

Source Link

Document

Create a new Cell style and add it to the workbook's style table.

Usage

From source file:Excel.Informesfacturables.java

public void GenerarInforme(String desde, String hasta) throws SQLException, IOException {
    HSSFWorkbook libro = new HSSFWorkbook();
    HSSFSheet hoja = libro.createSheet("Resumen");
    /*//from   w  w w . ja  v a  2 s.  com
     * GENERAR LAS SIGUIENTES HOJAS
     * 1- DETALLE DE MOVIMIENTOS DE CAJA - LEE EN MOVIMIENTOS CAJA INDENTIFICANDO EL TIPO DE MOVIMIENTO, USUARIOS Y 
     * NUMERO DE CAJA
     * 2- DETALLE DE ARTICULOS VENDIDOS: LISTADO DE MOVIEMIENTOS DE ARTICULOS, CON USUARIOS Y CAJA
     * 3- DETALLE DE GASTOS : MOVIMIENTOS DE CAJA DETALLANDO LOS GASTOS
     * 
     */

    String ttx = "celda numero :";
    HSSFRow fila = null;
    HSSFCell celda;
    HSSFCell celda1;
    HSSFCell celda2;
    HSSFCell celda3;
    HSSFCell celda4;
    HSSFCell celda5;
    HSSFCell celda6;
    HSSFCell celda7;
    HSSFCell celda8;
    HSSFFont fuente = libro.createFont();
    //fuente.setFontHeight((short)21);
    fuente.setFontName(fuente.FONT_ARIAL);
    fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    String form = null;
    String sql = "SELECT *,(select articulos.NOMBRE from articulos where articulos.ID=movimientosarticulosF.idArticulo)as descA,(select depositos.descripcion from depositos where depositos.numero=movimientosarticulosF.numeroDeposito)as depor  FROM movimientosarticulosF WHERE idArticulo > 1 and fecha between '"
            + desde + "' and '" + hasta + "'";
    System.out.println(sql);
    Transaccionable tra = new Conecciones();
    ResultSet rs = tra.leerConjuntoDeRegistros(sql);
    HSSFCellStyle titulo = libro.createCellStyle();
    titulo.setFont(fuente);
    //titulo.setFillBackgroundColor((short)22);
    titulo.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    titulo.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    //for(int a=0;a < 100;a++){
    int col = 0;
    int a = 0;
    Integer cantidad = 0;
    Double total = 0.00;
    if (a == 0) {
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        celda.setCellStyle(titulo);
        celda.setCellValue("Fecha");
        celda1 = fila.createCell(1);
        celda1.setCellStyle(titulo);
        celda1.setCellValue("Codigo");
        celda2 = fila.createCell(2);
        celda2.setCellStyle(titulo);
        celda2.setCellValue("Articulo");
        celda3 = fila.createCell(3);
        celda3.setCellStyle(titulo);
        celda3.setCellValue("Cantidad");
        celda4 = fila.createCell(4);
        celda4.setCellStyle(titulo);
        celda4.setCellValue("Precio Unitario");
        celda5 = fila.createCell(5);
        celda5.setCellStyle(titulo);
        celda5.setCellValue("Total");
        celda6 = fila.createCell(6);
        celda6.setCellStyle(titulo);
        celda6.setCellValue("idCaja");
        celda7 = fila.createCell(7);
        celda7.setCellStyle(titulo);
        celda7.setCellValue("Sucursal");
    }
    while (rs.next()) {
        a++;
        //col=rs.getInt("tipoMovimiento");
        switch (col) {
        case 1:

            break;
        default:

            break;
        }
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        ttx = ttx;
        celda.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda.setCellValue(rs.getString("fecha"));
        celda1 = fila.createCell(1);
        ttx = ttx;
        celda1.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda1.setCellValue(rs.getString("idArticulo"));
        celda2 = fila.createCell(2);
        celda2.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda2.setCellValue(rs.getString("descA"));
        celda3 = fila.createCell(3);
        cantidad = 0;
        total = 0.00;
        cantidad = rs.getInt("cantidad") * -1;
        total = rs.getDouble("precioDeVenta") * cantidad;
        celda3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda3.setCellValue(cantidad);
        celda4 = fila.createCell(4);
        celda4.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda4.setCellValue(rs.getDouble("precioDeVenta"));

        celda5 = fila.createCell(5);
        //celda5.setCellFormula(rs.getString("observaciones"));
        celda5.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda5.setCellValue(total);
        //celda5.setCellValue(rs.getDate("fecha"));
        celda6 = fila.createCell(6);
        celda6.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda6.setCellValue(rs.getInt("idcaja"));
        celda7 = fila.createCell(7);
        celda7.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda7.setCellValue(rs.getString("depor"));
    }

    rs.close();
    //texto+="\r\n";
    String ruta = "C://Informes//informeFacturables.xls";
    try {
        FileOutputStream elFichero = new FileOutputStream(ruta);
        try {
            libro.write(elFichero);
            elFichero.close();
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + ruta);
        } catch (IOException ex) {
            Logger.getLogger(Informesfacturables.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Informesfacturables.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Excel.InformesRemitosInternos.java

public void GenerarInforme(String desde, String hasta) throws SQLException, FileNotFoundException, IOException {
    HSSFWorkbook libro = new HSSFWorkbook();
    HSSFSheet hoja = libro.createSheet("Resumen");

    /*/*w  ww  .  ja va2s .  c  o  m*/
     * GENERAR LAS SIGUIENTES HOJAS
     * 1- DETALLE DE MOVIMIENTOS DE CAJA - LEE EN MOVIMIENTOS CAJA INDENTIFICANDO EL TIPO DE MOVIMIENTO, USUARIOS Y 
     * NUMERO DE CAJA
     * 2- DETALLE DE ARTICULOS VENDIDOS: LISTADO DE MOVIEMIENTOS DE ARTICULOS, CON USUARIOS Y CAJA
     * 3- DETALLE DE GASTOS : MOVIMIENTOS DE CAJA DETALLANDO LOS GASTOS
     * 
     */

    String ttx = "celda numero :";
    HSSFRow fila = null;
    HSSFCell celda;
    HSSFCell celda1;
    HSSFCell celda2;
    HSSFCell celda3;
    HSSFCell celda4;
    HSSFCell celda5;
    HSSFCell celda6;
    HSSFCell celda7;
    HSSFCell celda8;
    HSSFFont fuente = libro.createFont();
    //fuente.setFontHeight((short)21);
    fuente.setFontName(fuente.FONT_ARIAL);
    fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    String form = null;
    String sql = "SELECT *,(select depositos.descripcion from depositos where depositos.numero=movimientosdesucursales.depOrigen)as depor,(select depositos.descripcion from depositos where depositos.numero=movimientosdesucursales.depDestino)as depod,(select articulos.NOMBRE from articulos where articulos.ID=movimientosdesucursales.idArticulo)as descA  FROM movimientosdesucursales where fecha between '"
            + desde + "' and '" + hasta + "'";
    System.out.println(sql);
    Transaccionable tra = new Conecciones();
    ResultSet rs = tra.leerConjuntoDeRegistros(sql);
    HSSFCellStyle titulo = libro.createCellStyle();
    titulo.setFont(fuente);
    //titulo.setFillBackgroundColor((short)22);
    titulo.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    titulo.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    //for(int a=0;a < 100;a++){
    int col = 0;
    int a = 0;
    Double total = 0.00;
    if (a == 0) {
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        celda.setCellStyle(titulo);
        celda.setCellValue("Codigo");
        celda1 = fila.createCell(1);
        celda1.setCellStyle(titulo);
        celda1.setCellValue("Articulo");
        celda2 = fila.createCell(2);
        celda2.setCellStyle(titulo);
        celda2.setCellValue("Cantidad");
        celda3 = fila.createCell(3);
        celda3.setCellStyle(titulo);
        celda3.setCellValue("Costo");
        celda4 = fila.createCell(4);
        celda4.setCellStyle(titulo);
        celda4.setCellValue("Remito");
        celda5 = fila.createCell(5);
        celda5.setCellStyle(titulo);
        celda5.setCellValue("Origen");
        celda6 = fila.createCell(6);
        celda6.setCellStyle(titulo);
        celda6.setCellValue("Destino");
        celda7 = fila.createCell(7);
        celda7.setCellStyle(titulo);
        celda7.setCellValue("Fecha");
        celda8 = fila.createCell(8);
        celda8.setCellStyle(titulo);
        celda8.setCellValue("Total");
    }
    while (rs.next()) {
        a++;
        //col=rs.getInt("tipoMovimiento");
        switch (col) {
        case 1:

            break;
        default:

            break;
        }
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        ttx = ttx;
        total = 0.00;
        celda.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda.setCellValue(rs.getString("idArticulo"));
        celda1 = fila.createCell(1);
        ttx = ttx;
        celda1.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda1.setCellValue(rs.getString("descA"));
        celda2 = fila.createCell(2);
        celda2.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda2.setCellValue(rs.getInt("cantidad"));
        celda3 = fila.createCell(3);
        celda3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda3.setCellValue(rs.getDouble("costo"));
        total = rs.getInt("cantidad") * rs.getDouble("costo");
        celda4 = fila.createCell(4);
        celda4.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda4.setCellValue(rs.getInt("numeroRemito"));

        celda5 = fila.createCell(5);
        //celda5.setCellFormula(rs.getString("observaciones"));
        celda5.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda5.setCellValue(rs.getString("depor"));
        //celda5.setCellValue(rs.getDate("fecha"));
        celda6 = fila.createCell(6);
        celda6.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda6.setCellValue(rs.getString("depod"));
        celda7 = fila.createCell(7);
        celda7.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda7.setCellValue(rs.getString("fecha"));
        celda8 = fila.createCell(8);
        celda8.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda8.setCellValue(total);
    }

    rs.close();
    //texto+="\r\n";
    String ruta = "C://Informes//informedeRemitosInternos.xls";
    try {
        FileOutputStream elFichero = new FileOutputStream(ruta);
        try {
            libro.write(elFichero);
            elFichero.close();
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + ruta);
        } catch (IOException ex) {
            Logger.getLogger(InformesRemitosInternos.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(InformesRemitosInternos.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Excel.IvaVentas.java

public void GenerrarInformeIvaVentas(String desde, String hasta) throws SQLException {
    HSSFWorkbook libro = new HSSFWorkbook();
    HSSFSheet hoja = libro.createSheet("Iva Ventas");
    /*/*w  ww. ja v  a 2  s. co  m*/
     * GENERAR LAS SIGUIENTES HOJAS
     * 1- DETALLE DE MOVIMIENTOS DE CAJA - LEE EN MOVIMIENTOS CAJA INDENTIFICANDO EL TIPO DE MOVIMIENTO, USUARIOS Y 
     * NUMERO DE CAJA
     * 2- DETALLE DE ARTICULOS VENDIDOS: LISTADO DE MOVIEMIENTOS DE ARTICULOS, CON USUARIOS Y CAJA
     * 3- DETALLE DE GASTOS : MOVIMIENTOS DE CAJA DETALLANDO LOS GASTOS
     * 
     */

    String ttx = "celda numero :";
    HSSFRow fila = null;
    HSSFCell celda;
    HSSFCell celda1;
    HSSFCell celda2;
    HSSFCell celda3;
    HSSFCell celda4;
    HSSFCell celda5;
    HSSFCell celda6;
    HSSFCell celda7;
    HSSFCell celda8;
    HSSFFont fuente = libro.createFont();
    //fuente.setFontHeight((short)21);
    fuente.setFontName(fuente.FONT_ARIAL);
    fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    String form = null;
    String sql = "SELECT *,round(gravado,2)as gravadoR,round(impuesto,2)as impuestoR,round(total,2)as totalR FROM fiscal where fechaRegistro between '"
            + desde + "' and '" + hasta + "' group by numero order by numero";
    //String sql="SELECT *,round(gravado,2)as gravadoR,round(impuesto,2)as impuestoR,round(total,2)as totalR FROM fiscal where fecha like '201607%' group by numero order by numero";
    System.out.println(sql);
    Transaccionable tra = new ConeccionLocal();
    ResultSet rs = tra.leerConjuntoDeRegistros(sql);
    HSSFCellStyle titulo = libro.createCellStyle();
    titulo.setFont(fuente);
    //titulo.setFillBackgroundColor((short)22);
    titulo.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    titulo.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    //for(int a=0;a < 100;a++){
    int col = 0;
    int a = 0;
    if (a == 0) {
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        celda.setCellStyle(titulo);
        celda.setCellValue("Fecha");
        celda1 = fila.createCell(1);
        celda1.setCellStyle(titulo);
        celda1.setCellValue("Tipo comp.");
        celda2 = fila.createCell(2);
        celda2.setCellStyle(titulo);
        celda2.setCellValue("Numero");
        celda3 = fila.createCell(3);
        celda3.setCellStyle(titulo);
        celda3.setCellValue("Gravado");
        celda4 = fila.createCell(4);
        celda4.setCellStyle(titulo);
        celda4.setCellValue("Impuesto");
        celda5 = fila.createCell(5);
        celda5.setCellStyle(titulo);
        celda5.setCellValue("Total");
        celda6 = fila.createCell(6);
        celda6.setCellStyle(titulo);
        celda6.setCellValue("Razon Social");
    }
    while (rs.next()) {
        a++;
        //col=rs.getInt("tipoMovimiento");
        switch (col) {
        case 1:

            break;
        default:

            break;
        }
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        ttx = ttx;
        celda.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda.setCellValue(rs.getString("fecha"));
        celda1 = fila.createCell(1);
        ttx = ttx;
        celda1.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda1.setCellValue(rs.getInt("tipo"));
        celda2 = fila.createCell(2);
        celda2.setCellType(HSSFCell.CELL_TYPE_STRING);
        String numero = rs.getString("numero").replaceFirst("80", "00");
        celda2.setCellValue(numero);
        celda3 = fila.createCell(3);
        celda3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda3.setCellValue(rs.getDouble("gravadoR"));
        celda4 = fila.createCell(4);
        celda4.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda4.setCellValue(rs.getDouble("impuestoR"));

        celda5 = fila.createCell(5);
        //celda5.setCellFormula(rs.getString("observaciones"));
        celda5.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda5.setCellValue(rs.getDouble("totalR"));
        //celda5.setCellValue(rs.getDate("fecha"));
        celda6 = fila.createCell(6);
        celda6.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda6.setCellValue(rs.getString("razon"));
    }

    rs.close();
    //texto+="\r\n";
    String ruta = "fiscal/" + desde + "_" + hasta + " Iva Ventas.xls";
    try {
        FileOutputStream elFichero = new FileOutputStream(ruta);
        try {
            libro.write(elFichero);
            elFichero.close();
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + ruta);
        } catch (IOException ex) {
            Logger.getLogger(InformeMensual.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(InformeMensual.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Excel.PlanillaStock.java

public void GenerarInforme() throws SQLException {
    HSSFWorkbook libro = new HSSFWorkbook();
    HSSFSheet hoja = libro.createSheet("Listado de Articulos");
    ArrayList listadoPorSucursal = new ArrayList();
    Editables edi = new Articulos();

    /*/* www . j a  v a2 s.  co m*/
     * GENERAR LAS SIGUIENTES HOJAS
     * 1- DETALLE DE MOVIMIENTOS DE CAJA - LEE EN MOVIMIENTOS CAJA INDENTIFICANDO EL TIPO DE MOVIMIENTO, USUARIOS Y 
     * NUMERO DE CAJA
     * 2- DETALLE DE ARTICULOS VENDIDOS: LISTADO DE MOVIEMIENTOS DE ARTICULOS, CON USUARIOS Y CAJA
     * 3- DETALLE DE GASTOS : MOVIMIENTOS DE CAJA DETALLANDO LOS GASTOS
     * 
     */

    String ttx = "celda numero :";
    HSSFRow fila = null;
    HSSFCell celda;
    HSSFCell celda1;
    HSSFCell celda2;
    HSSFCell celda3;
    HSSFCell celda4;
    HSSFCell celda5;
    HSSFCell celda6;
    HSSFCell celda7;
    HSSFCell celda8;
    HSSFCell celda9;
    HSSFCell celda10;
    HSSFCell celda11;
    HSSFFont fuente = libro.createFont();
    //fuente.setFontHeight((short)21);
    fuente.setFontName(fuente.FONT_ARIAL);
    fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    String form = null;

    HSSFCellStyle titulo = libro.createCellStyle();
    //Iterator iCli=listadoClientes.listIterator();
    Articulos cliente;
    titulo.setFont(fuente);
    //titulo.setFillBackgroundColor((short)22);
    titulo.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    titulo.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFCellStyle celdas = libro.createCellStyle();
    celdas.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
    celdas.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
    celdas.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
    celdas.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
    celdas.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    celdas.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    celdas.setRightBorderColor(IndexedColors.BLACK.getIndex());
    celdas.setTopBorderColor(IndexedColors.BLACK.getIndex());
    int a = 0;
    String sql = "select *,(select sum(cantidad) FROM movimientosarticulos where movimientosarticulos.idArticulo=articulos.ID group by idArticulo,numeroDeposito limit 0,1)as sstock,round((articulos.dolar * articulos.PRECIO),2)as pl1,round((articulos.dolar * articulos.lista2),2)as pl2,round((articulos.dolar * articulos.lista3),2)as pl3,round((articulos.lista4 * articulos.dolar),2)as pl4,round((articulos.dolar * articulos.COSTO),2)as pcto from articulos where (select sum(cantidad) FROM movimientosarticulos where movimientosarticulos.idArticulo=articulos.ID group by idArticulo limit 0,1) > 0 order by sstock desc";

    Transaccionable tra1 = new ConeccionLocal();
    ResultSet rr1 = tra1.leerConjuntoDeRegistros(sql);
    //Facturar fact=new Articulos();
    //ArrayList listadoClientes=fact.listadoBusqueda("");
    System.out.println("cantidad registros " + rr1.getRow());
    while (rr1.next()) {
        int col = 0;
        //int a=0;
        if (a == 0) {
            fila = hoja.createRow(a);
            celda = fila.createCell(0);
            celda.setCellStyle(titulo);
            celda.setCellValue("Codigo");
            celda1 = fila.createCell(1);
            celda1.setCellStyle(titulo);
            celda1.setCellValue("Descripcion");
            celda2 = fila.createCell(2);
            celda2.setCellStyle(titulo);
            celda2.setCellValue("cod. barras");
            celda3 = fila.createCell(3);
            celda3.setCellStyle(titulo);
            celda3.setCellValue("Cantidad");

            a++;
        }
        // cliente=new Articulos();
        //cliente=(Articulos)listadoClientes.get(a);
        fila = hoja.createRow(a);
        celda = fila.createCell(0);
        ttx = ttx;
        celda.setCellStyle(celdas);
        celda.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda.setCellValue(rr1.getString("ID"));

        celda1 = fila.createCell(1);
        ttx = ttx;
        celda1.setCellStyle(celdas);
        celda1.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda1.setCellValue(rr1.getString("NOMBRE"));

        celda2 = fila.createCell(2);
        celda2.setCellStyle(celdas);
        celda2.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda2.setCellValue(rr1.getString("BARRAS"));

        celda3 = fila.createCell(3);
        celda3.setCellStyle(celdas);
        celda3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        celda3.setCellValue(rr1.getDouble("sstock"));

        celda4 = fila.createCell(4);
        celda4.setCellStyle(celdas);
        celda4.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda4.setCellValue("");

        celda5 = fila.createCell(5);
        celda5.setCellStyle(celdas);
        //celda5.setCellFormula(rs.getString("observaciones"));
        celda5.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda5.setCellValue("");
        //celda5.setCellValue(rs.getDate("fecha"));

        celda6 = fila.createCell(6);
        //celda5.setCellFormula(rs.getString("observaciones"));
        celda6.setCellStyle(celdas);
        celda6.setCellType(HSSFCell.CELL_TYPE_STRING);
        celda6.setCellValue("");
        //listadoPorSucursal=edi.ListarPorSucursal(cliente);
        //Iterator il=listadoPorSucursal.listIterator();

        a++;
    }
    rr1.close();

    String ruta = "Informes\\planillaStock.xls";
    try {
        FileOutputStream elFichero = new FileOutputStream(ruta);
        try {
            libro.write(elFichero);
            elFichero.close();
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + ruta);
        } catch (IOException ex) {
            Logger.getLogger(InformeMensual.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(InformeMensual.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:excel.PoiWriteExcelFile.java

public static int generarReporte() {

    //Calendar cal=Calendar.getInstance();
    Calendar cal = WorkMonitorUI.instante;

    try {/*from w w  w  .j a  v  a2  s . co m*/
        FileOutputStream fileOut = new FileOutputStream("HH_"
                + instante.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.getDefault())
                        .toUpperCase()
                + "_" + persona.getNombre().toUpperCase().charAt(0) + "." + persona.getApellido().toUpperCase()
                + "_" + instante.get(Calendar.YEAR) + ".xls");
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet worksheet = workbook.createSheet(
                cal.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.getDefault()).toUpperCase()
                        + "-" + cal.get(Calendar.YEAR));

        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("Calibri");
        font.setItalic(false);
        font.setBold(true);
        font.setColor(HSSFColor.BLACK.index);
        cellStyle.setFont(font);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle diasStyle = workbook.createCellStyle();
        diasStyle.setFillForegroundColor(HSSFColor.SEA_GREEN.index);
        diasStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        font = workbook.createFont();
        font.setFontHeightInPoints((short) 11);
        font.setFontName("Calibri");
        font.setItalic(false);
        font.setBold(true);
        font.setColor(HSSFColor.WHITE.index);
        diasStyle.setFont(font);
        diasStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        diasStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        diasStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        diasStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        diasStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        diasStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle schedStyle = workbook.createCellStyle();
        schedStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        schedStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font3 = workbook.createFont();
        font3.setFontHeightInPoints((short) 11);
        font3.setFontName("Calibri");
        font3.setItalic(false);
        font3.setColor(HSSFColor.BLACK.index);
        schedStyle.setFont(font3);
        schedStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        schedStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        schedStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        schedStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        schedStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        schedStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle workdayStyle = workbook.createCellStyle();
        //workdayStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);                        
        workdayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        workdayStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        workdayStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        workdayStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        workdayStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        workdayStyle.setWrapText(true);
        HSSFFont font2 = workbook.createFont();
        font2.setFontHeightInPoints((short) 8);
        font2.setFontName("Serif");
        font2.setItalic(false);
        //font2.setColor(HSSFColor.YELLOW.index);
        workdayStyle.setFont(font2);
        workdayStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle weekendStyle = workbook.createCellStyle();
        weekendStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        weekendStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        weekendStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        weekendStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        weekendStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        weekendStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        weekendStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        weekendStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle horarioStyle = workbook.createCellStyle();
        horarioStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        horarioStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        horarioStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        horarioStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        horarioStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        horarioStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        horarioStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        horarioStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont font4 = workbook.createFont();
        font4.setFontHeightInPoints((short) 10);
        font4.setFontName("Serif");
        font4.setItalic(false);
        font4.setBold(true);
        //font2.setColor(HSSFColor.YELLOW.index);
        horarioStyle.setFont(font4);

        // index from 0,0... cell A1 is cell(0,0)
        HSSFRow row1 = worksheet.createRow((short) 0);
        row1.setHeight((short) 500);

        //System.out.println("cal.get(Calendar.YEAR)="+cal.get(Calendar.YEAR));

        HSSFCell cellA1 = row1.createCell((short) 0);
        cellA1.setCellValue(
                cal.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.getDefault()).toUpperCase()
                        + "-" + cal.get(Calendar.YEAR));
        cellA1.setCellStyle(cellStyle);

        HSSFRow row2 = worksheet.createRow((short) 1);
        HSSFCell cellA4 = row2.createCell((short) 0);
        cellA4.setCellValue("Horario");
        cellA4.setCellStyle(horarioStyle);
        //row2.setHeight((short)500);

        HSSFRow row3 = worksheet.createRow((short) 2);
        HSSFCell cellA3 = row3.createCell((short) 0);
        cellA3.setCellValue("Inicio - Trmino");
        cellA3.setCellStyle(diasStyle);

        Calendar hora = Calendar.getInstance();

        hora.set(Calendar.HOUR_OF_DAY, 9);
        hora.set(Calendar.MINUTE, 0);
        hora.set(Calendar.SECOND, 0);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

        HSSFCell cellXn;

        for (int i = 0; i < 29; ++i) {
            HSSFRow row = worksheet.createRow((short) i + 3);
            row.setHeight((short) 500);

            cellXn = row.createCell((short) 0);
            String horaIni = sdf.format(hora.getTime());
            hora.add(Calendar.MINUTE, 30);
            String horaFin = sdf.format(hora.getTime());
            cellXn.setCellValue(horaIni + " - " + horaFin);
            cellXn.setCellStyle(schedStyle);
        }

        System.out.println("cal.get(Calendar.MONTH)1=" + cal.get(Calendar.MONTH));

        cal.add(Calendar.DAY_OF_MONTH, -cal.get(Calendar.DAY_OF_MONTH) + 1);

        int diasMes = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        System.out.println("cal.get(Calendar.MONTH)2=" + cal.get(Calendar.MONTH));

        sdf = new SimpleDateFormat("EEEE d");

        System.out.println(
                "cal.getActualMaximum(Calendar.DAY_OF_MONTH)1=" + cal.getActualMaximum(Calendar.DAY_OF_MONTH));

        for (int i = 0; i < diasMes; ++i) {
            cellXn = row2.createCell((short) i + 1);
            String dia = sdf.format(cal.getTime());
            dia = Character.toUpperCase(dia.charAt(0)) + dia.substring(1);
            cellXn.setCellValue(dia);
            cellXn.setCellStyle(horarioStyle);
            //System.out.println("cal.get(Calendar.DAY_OF_MONTH)="+cal.get(Calendar.DAY_OF_MONTH));
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }

        for (int i = 0; i < diasMes; ++i) {
            cellXn = row3.createCell((short) i + 1);
            cellXn.setCellValue("Descripcin");
            cellXn.setCellStyle(diasStyle);
        }

        System.out.println(
                "cal.getActualMaximum(Calendar.DAY_OF_MONTH)2=" + cal.getActualMaximum(Calendar.DAY_OF_MONTH));

        // Retroceder mes para que quede como estaba
        cal.add(Calendar.MONTH, -1);
        //cal.add(Calendar.DAY_OF_MONTH, -1);    

        System.out.println(
                "cal.getActualMaximum(Calendar.DAY_OF_MONTH)3=" + cal.getActualMaximum(Calendar.DAY_OF_MONTH));

        HhDao hhDao = new HhDao();
        Object[][] hh = new Object[29][cal.getActualMaximum(Calendar.DAY_OF_MONTH)];

        hh = hhDao.getByMes(WorkMonitorUI.persona.getId(), cal.getTime());

        cal.set(Calendar.DAY_OF_MONTH, 1);

        Sheet sheet = workbook.getSheetAt(0);

        sdf = new SimpleDateFormat("EEEE");

        HSSFPatriarch _drawing = (HSSFPatriarch) sheet.createDrawingPatriarch();
        CreationHelper factory = workbook.getCreationHelper();

        for (int i = 0; i < 29; ++i) {
            Row r = sheet.getRow(i + 3);
            for (int j = 0; j < diasMes; ++j) {
                if (hh[i][j].toString() != "") {
                    cellXn = (HSSFCell) r.createCell((short) j + 1);
                    Hh _hh = (Hh) hh[i][j];
                    cellXn.setCellValue(
                            _hh.getTarea().getNombre().trim() + ": " + _hh.getActividad().getNombre().trim());

                    HSSFAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5);
                    org.apache.poi.ss.usermodel.Comment comment = _drawing.createComment(anchor);
                    String comentario = _hh.getTarea().getComentario().toLowerCase();
                    if (_hh.getComentario() != null)
                        comentario = comentario + _hh.getComentario().toLowerCase();
                    RichTextString str = factory.createRichTextString(comentario);

                    comment.setString(str);

                    cellXn.setCellComment(comment);
                } else {
                    cellXn = (HSSFCell) r.createCell((short) j + 1);
                    cellXn.setCellValue("");
                }
                //System.out.println("sdf.format(cal.getTime())="+sdf.format(cal.getTime()));
                if (Arrays.asList("sbado", "domingo").contains(sdf.format(cal.getTime())))
                    cellXn.setCellStyle(weekendStyle);
                else
                    cellXn.setCellStyle(workdayStyle);
                sheet.setColumnWidth(j, 5000);

                cal.add(Calendar.DAY_OF_MONTH, 1);
                //sheet.autoSizeColumn(j);
            }
            // Retroceder mes para que quede como estaba                
            cal.add(Calendar.MONTH, -1);
            System.out.println("cal.get(Calendar.MONTH)3=" + cal.get(Calendar.MONTH));
            cal.set(Calendar.DAY_OF_MONTH, 1);
        }
        sheet.setColumnWidth(diasMes, 5000);

        WorkMonitorUI.instante = Calendar.getInstance();
        sheet.setColumnWidth(0, 5000);
        sheet.createFreezePane(1, 3);
        // Freeze just one row
        //sheet.createFreezePane( 0, 1, 0, 1 );

        workbook.write(fileOut);
        fileOut.flush();
        fileOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return -1;
    } catch (IOException e) {
        e.printStackTrace();
        return -2;
    }
    return 1;
}

From source file:exportToExcel.ResultSetToExcel.java

License:Open Source License

public HSSFWorkbook dump(ResultSet resultSet) throws SQLException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFFont boldFont = workbook.createFont();
    boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    HSSFSheet sheet = workbook.createSheet("sheet");
    HSSFRow titleRow = sheet.createRow(0);
    ResultSetMetaData metaData = resultSet.getMetaData();
    int columnCount = metaData.getColumnCount();
    HSSFDataFormat dataFormat = workbook.createDataFormat();
    HSSFCellStyle[] dataStyles = new HSSFCellStyle[columnCount + 1];
    for (int colIndex = 0; colIndex < columnCount; colIndex++) {
        String title = metaData.getColumnLabel(colIndex + 1);
        HSSFCell cell = HSSFCellUtil.createCell(titleRow, colIndex, title);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFont(boldFont);/*from  www.  ja  va  2  s. com*/
        cell.setCellStyle(style);
        HSSFCellStyle dataStyle = getDataStyle(workbook, metaData, colIndex, dataFormat);
        dataStyles[colIndex] = dataStyle;
    }
    dumpData(resultSet, sheet, columnCount, dataStyles);
    return workbook;
}

From source file:exportToExcel.ResultSetToExcel.java

License:Open Source License

private HSSFCellStyle getDataStyle(HSSFWorkbook workbook, ResultSetMetaData metaData, int colIndex,
        HSSFDataFormat dataFormat) throws SQLException {
    HSSFCellStyle dataStyle = workbook.createCellStyle();
    String columnType = metaData.getColumnTypeName(colIndex + 1);
    columnType += "(" + metaData.getPrecision(colIndex + 1);
    columnType += "," + metaData.getScale(colIndex + 1) + ")";
    String excelFormat = getExcelFormat(columnType);
    final short format = dataFormat.getFormat(excelFormat);
    dataStyle.setDataFormat(format);/*from  www.jav a 2 s.  com*/
    return dataStyle;
}

From source file:featurescomparison.workingwithformattingfeatures.cellalignment.java.ApacheCellAlignment.java

License:Apache License

/**
 * Creates a cell and aligns it a certain way.
 *
 * @param wb        the workbook/*from   w w w  .  j  a va 2 s  . c o m*/
 * @param row       the row to create the cell in
 * @param column    the column number to create the cell in
 * @param align     the alignment for the cell.
 */
private static void createCell(HSSFWorkbook wb, HSSFRow row, int column, int align) {
    HSSFCell cell = row.createCell(column);
    cell.setCellValue("Align It");
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment((short) align);
    cell.setCellStyle(cellStyle);
}

From source file:fileXLS.MakeFileXLS.java

public void resources(int id, int stock_id) throws IOException, Exception {
    DBEntry[] params;// ww  w . j  a v  a2s .  c  o m
    if (stock_id == -1 && id != -1) {
        params = new DBEntry[1];
        params[0] = new DBEntry("resource_id", EntryType.Int, id);
    } else if (stock_id != -1 && id != -1) {
        params = new DBEntry[2];
        AvailableResource avRes = AvailableResource.getOne(id);
        params[0] = new DBEntry("resource_id", EntryType.Int, avRes.getResourceId());
        params[1] = new DBEntry("stock_id", EntryType.Int, stock_id);
    } else if (stock_id != -1 && id == -1) {
        params = new DBEntry[1];
        params[0] = new DBEntry("stock_id", EntryType.Int, stock_id);
    } else {
        params = null;
    }
    History[] history = History.getAll(params);
    String[] columnNames = { ", --", "??", "-",
            "", "" };
    Object[][] data = new Object[history.length][5];
    int len = Resource.getAll(null)[Resource.getAll(null).length - 1].getId();
    int[] total = new int[len];
    for (int i = 0; i < len; i++)
        total[i] = 0;
    for (int i = 0; i < history.length; i++) {
        data[i][0] = history[i].getDateString();
        Resource res = Resource.getOne(history[i].getResourceId());
        data[i][1] = res.getName();
        total[history[i].getResourceId() - 1] += history[i].getNumber();
        data[i][2] = total[history[i].getResourceId() - 1] + " " + res.getMeasureName();
        if (history[i].getNumber() > 0)
            data[i][3] = "+" + history[i].getNumber() + " " + res.getMeasureName();
        else
            data[i][3] = history[i].getNumber() + " " + res.getMeasureName();
        data[i][4] = "" + history[i].getStockId();
    }
    JTable jtable = new JTable(data, columnNames);
    HSSFWorkbook table = new HSSFWorkbook();
    HSSFSheet fSheet = table.createSheet("??");
    String link1 = link;
    if (stock_id == -1 && id != -1)
        link1 += "resourceId" + id + ".xls";
    else if (stock_id != -1 && id != -1)
        link1 += "resourceId" + id + "&stockId" + stock_id + ".xls";
    else if (stock_id != -1 && id == -1)
        link1 += "stockId" + stock_id + ".xls";
    else
        link1 += "history.xls";
    File file = new File(link1);
    HSSFCellStyle cellStyle = table.createCellStyle();
    TableModel model = jtable.getModel();
    HSSFRow fRow = fSheet.createRow(0);
    for (int j = 0; j < model.getColumnCount(); j++) {
        HSSFCell cell = fRow.createCell(j);
        cell.setCellValue(jtable.getColumnName(j));
    }
    for (int i = 0; i < model.getRowCount(); i++) {
        fRow = fSheet.createRow(i + 1);
        for (int j = 0; j < model.getColumnCount(); j++) {
            HSSFCell cell = fRow.createCell(j);
            cell.setCellValue(model.getValueAt(i, j).toString());
            cell.setCellStyle(cellStyle);
        }
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
    table.write(bos);
    bos.close();
    fileOutputStream.close();
}

From source file:fileXLS.MakeFileXLS.java

public void applications(int status) throws Exception {
    DBEntry[] params = { new DBEntry("request_type_id", EntryType.Int, 1),
            new DBEntry("status", EntryType.Int, status) };
    DBEntry[] params1 = { new DBEntry("request_type_id", EntryType.Int, 2),
            new DBEntry("status", EntryType.Int, status) };
    Request[] req = Request.getAll(params);
    Request[] req1 = Request.getAll(params1);
    Vector<Integer> ind = new Vector();
    Vector<Integer> numb = new Vector();
    for (int i = 0; i < req.length; i++) {
        if (ind.size() > 0) {
            boolean f = false;
            for (int j = 0; j < ind.size(); j++)
                if (ind.elementAt(j) == req[i].getResourceId()) {
                    numb.set(j, numb.elementAt(j) + req[i].getNumber());
                    f = true;/* w w  w . j a  va2  s  . co m*/
                }
            if (!f) {
                numb.add(req[i].getNumber());
                ind.add(req[i].getResourceId());
            }
        } else {
            numb.add(req[i].getNumber());
            ind.add(req[i].getResourceId());
        }
    }
    Vector<Integer> ind1 = new Vector();
    Vector<Integer> numb1 = new Vector();
    for (int i = 0; i < req1.length; i++) {
        if (ind1.size() > 0) {
            boolean f = false;
            for (int j = 0; j < ind1.size(); j++)
                if (ind1.elementAt(j) == req1[i].getResourceId()) {
                    numb1.set(j, numb1.elementAt(j) + req1[i].getNumber());
                    f = true;
                }
            if (!f) {
                numb1.add(req1[i].getNumber());
                ind1.add(req1[i].getResourceId());
            }
        } else {
            numb1.add(req1[i].getNumber());
            ind1.add(req1[i].getResourceId());
        }
    }
    String[] columnNames = { "??", "-" };
    Object[][] data = new Object[numb.size()][2];
    Object[][] data1 = new Object[numb1.size()][2];
    for (int i = 0; i < numb.size(); i++) {
        data[i][0] = Resource.getOne(ind.elementAt(i)).getName();
        data[i][1] = numb.elementAt(i) + " " + Resource.getOne(ind.elementAt(i)).getMeasureName();
    }
    for (int i = 0; i < numb1.size(); i++) {
        data1[i][0] = Resource.getOne(ind1.elementAt(i)).getName();
        data1[i][1] = numb1.elementAt(i) + " " + Resource.getOne(ind1.elementAt(i)).getMeasureName();
    }
    JTable jtable = new JTable(data, columnNames);
    JTable jtable1 = new JTable(data1, columnNames);
    HSSFWorkbook table = new HSSFWorkbook();
    HSSFSheet fSheet = table.createSheet("??? ??");
    HSSFSheet fSheet1 = table.createSheet(" ??");
    String link1 = link;
    if (status == 0)
        link1 += "resources_ready.xls";
    else
        link1 += "resources_done.xls";
    File file = new File(link1);
    HSSFCellStyle cellStyle = table.createCellStyle();
    TableModel model = jtable.getModel();
    TableModel model1 = jtable1.getModel();
    HSSFRow fRow = fSheet.createRow(0);
    for (int j = 0; j < model.getColumnCount(); j++) {
        HSSFCell cell = fRow.createCell(j);
        cell.setCellValue(jtable.getColumnName(j));
    }
    for (int i = 0; i < model.getRowCount(); i++) {
        fRow = fSheet.createRow(i + 1);
        for (int j = 0; j < model.getColumnCount(); j++) {
            HSSFCell cell = fRow.createCell(j);
            cell.setCellValue(model.getValueAt(i, j).toString());
            cell.setCellStyle(cellStyle);
        }
    }
    HSSFRow fRow1 = fSheet1.createRow(0);
    for (int j = 0; j < model1.getColumnCount(); j++) {
        HSSFCell cell = fRow1.createCell(j);
        cell.setCellValue(jtable1.getColumnName(j));
    }
    for (int i = 0; i < model1.getRowCount(); i++) {
        fRow1 = fSheet1.createRow(i + 1);
        for (int j = 0; j < model1.getColumnCount(); j++) {
            HSSFCell cell = fRow1.createCell(j);
            cell.setCellValue(model1.getValueAt(i, j).toString());
            cell.setCellStyle(cellStyle);
        }
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
    table.write(bos);
    bos.close();
    fileOutputStream.close();
}