Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle setBorderBottom

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle setBorderBottom

Introduction

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

Prototype

@Override
public void setBorderBottom(BorderStyle border) 

Source Link

Document

set the type of border to use for the bottom border of the cell

Usage

From source file:neg.JRViewerComercialProveedor.java

License:Open Source License

private void btnExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcelActionPerformed

    String queryInf = this.jasperPrint.getProperty("query");

    try {/*from  w  ww .  ja va  2 s  .c  om*/
        // Se crea el libro excel
        HSSFWorkbook libro = new HSSFWorkbook();
        //Se crea la hoja
        HSSFSheet hoja = libro.createSheet("Informe");
        //Numero de fila de la hoja Excel
        int num_fila = 1;
        crearCabeceraHojaExcel(libro, hoja);

        HSSFCellStyle cs2 = libro.createCellStyle();

        cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs2.setBottomBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs2.setLeftBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs2.setRightBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs2.setTopBorderColor(HSSFColor.BLACK.index);

        HSSFCellStyle cs3 = libro.createCellStyle();
        HSSFDataFormat format = libro.createDataFormat();
        cs3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        cs3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs3.setBottomBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs3.setLeftBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs3.setRightBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs3.setTopBorderColor(HSSFColor.BLACK.index);
        cs3.setDataFormat(format.getFormat("#,##0"));

        //Datos
        ResultSet rs = CSDesktop.datos.select(queryInf);

        crearFilaHojaExcel(libro, hoja, num_fila, rs, cs2, cs3);
        FileOutputStream elFichero = null;
        elFichero = new FileOutputStream("c:\\AplicacionCarSet\\informe_comercial_proveedor.xls");
        libro.write(elFichero);
        elFichero.close();
        elFichero.flush();
        String property = "java.io.tmpdir";
        String tempDir = System.getProperty(property);
        System.out.println("OS current temporary directory is " + tempDir);
        String file = new String("C:\\AplicacionCarSet\\informe_comercial_proveedor.xls");
        Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file);

    } catch (SQLException ex) {
        Logger.getLogger(CSResultBuscarPedidoNew.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnknownHostException ex) {
        Logger.getLogger(CSResultBuscarPedidoNew.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CSResultBuscarPedidoNew.class.getName()).log(Level.SEVERE, null, ex);
    }

    //System.out.println("Vamoooooos: "+queryInf);

}

From source file:neg.JRViewerComercialProveedor.java

License:Open Source License

private static void crearCabeceraHojaExcel(HSSFWorkbook libro, HSSFSheet hoja) {
    HSSFRow fila = null;//www. jav a  2  s  .  com
    HSSFCell celda = null;

    // Modificamos la fuente por defecto para que salga en negrita
    HSSFCellStyle cs = libro.createCellStyle();
    HSSFFont f = libro.createFont();
    f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    f.setColor(HSSFColor.WHITE.index);
    cs.setFont(f);
    //cs.setFillBackgroundColor(HSSFColor.GREEN.index);
    cs.setFillForegroundColor(HSSFColor.GREEN.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cs.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
    cs.setBottomBorderColor(HSSFColor.BLACK.index);
    cs.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
    cs.setLeftBorderColor(HSSFColor.BLACK.index);
    cs.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
    cs.setRightBorderColor(HSSFColor.BLACK.index);
    cs.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
    cs.setTopBorderColor(HSSFColor.BLACK.index);

    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    // Creamos la cabecera de las columnas
    fila = hoja.createRow(0);

    celda = fila.createCell((short) 0);
    celda.setCellStyle(cs);
    HSSFRichTextString texto = new HSSFRichTextString("PROVEEDOR");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 0, (short) ((220 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 1);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ENERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 1, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 2);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("FEBRERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 2, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 3);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MARZO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 3, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 4);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ABRIL");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 4, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 5);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MAYO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 5, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 6);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JUNIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 6, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 7);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JULIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 7, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 8);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("AGOSTO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 8, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 9);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("SEPTIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 9, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 10);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("OCTUBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 10, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 11);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NOVIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 11, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 12);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("DICIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 12, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 13);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("IMPORTE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 13, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 14);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("% TURISMOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 14, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 15);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("% FUNCIONAN");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 15, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 16);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("%GRA UNITARIA");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 16, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 17);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NM.PEDIDOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 17, (short) ((90 * 2) / ((double) 1 / 20)));

}

From source file:neg.JRViewerComercialProveedor.java

License:Open Source License

private static void crearFilaHojaExcel(HSSFWorkbook libro, HSSFSheet hoja, int num_fila, ResultSet rs,
        HSSFCellStyle cs2, HSSFCellStyle cs3) throws SQLException, UnknownHostException {
    HSSFRow fila = null;/*from w ww.j a  v  a 2 s .  co m*/
    HSSFCell celda = null;
    HSSFRichTextString texto = null;
    int num_fila_aux = 2;
    while (rs.next()) {
        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(num_fila);

        //Celda del Tarifa Cliente
        HSSFDataFormat format = libro.createDataFormat();

        HSSFCellStyle style = libro.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setTopBorderColor(HSSFColor.BLACK.index);

        style.setDataFormat(format.getFormat("#,##0"));

        //Celda de Proveedor
        celda = fila.createCell((short) 0);
        celda.setCellStyle(cs2);
        String proveedor = rs.getString("pr_nombre_fiscal");
        texto = new HSSFRichTextString(proveedor);
        celda.setCellStyle(cs3);
        celda.setCellValue(texto);

        celda.setCellStyle(style);

        //Celda de enero
        celda = fila.createCell((short) 1);
        celda.setCellStyle(cs3);
        double enero = rs.getDouble("enero_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(enero);
        style.setDataFormat(format.getFormat("00"));

        //Celda de febrero
        celda = fila.createCell((short) 2);
        double febrero = rs.getDouble("febrero_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(febrero);
        style.setDataFormat(format.getFormat("00"));

        //Celda de marzo
        celda = fila.createCell((short) 3);
        celda.setCellStyle(cs2);
        double marzo = rs.getDouble("marzo_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(marzo);
        style.setDataFormat(format.getFormat("00"));

        //Celda de abril
        celda = fila.createCell((short) 4);
        double abril = rs.getDouble("abril_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(abril);
        style.setDataFormat(format.getFormat("00"));

        //Celda de mayo
        celda = fila.createCell((short) 5);
        double mayo = rs.getDouble("mayo_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(mayo);
        style.setDataFormat(format.getFormat("00"));

        //Celda de junio
        celda = fila.createCell((short) 6);
        double junio = rs.getDouble("junio_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(junio);

        //Celda del julio
        celda = fila.createCell((short) 7);
        double julio = rs.getDouble("julio_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(julio);
        style.setDataFormat(format.getFormat("00"));

        //Celda de agosto
        celda = fila.createCell((short) 8);
        celda.setCellStyle(cs2);
        double agosto = rs.getDouble("agosto_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(agosto);
        style.setDataFormat(format.getFormat("00"));

        //Celda de septiembre
        celda = fila.createCell((short) 9);
        double septiembre = rs.getDouble("septiembre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(septiembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de octubre
        celda = fila.createCell((short) 10);
        celda.setCellStyle(cs2);
        double octubre = rs.getDouble("octubre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(octubre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de noviembre
        celda = fila.createCell((short) 11);
        double noviembre = rs.getDouble("noviembre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(noviembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de diciembre
        celda = fila.createCell((short) 12);
        celda.setCellStyle(cs2);
        double diciembre = rs.getDouble("diciembre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(diciembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de la tarifa de Proveedor
        celda = fila.createCell((short) 13);
        celda.setCellValue(rs.getDouble("ta_proveedor"));

        //Celda de % turismo
        celda = fila.createCell((short) 14);
        celda.setCellStyle(cs3);
        double t_proveedor = Double.parseDouble(rs.getString("ta_proveedor"));
        double factor = Integer.parseInt(rs.getString("factor"));

        int num_pedido = Integer.parseInt(rs.getString("num_pedido"));
        String turismo = (!rs.getString("factor").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format((factor / num_pedido) * 100).concat(" % ")
                : "0 % ";
        style.setDataFormat(format.getFormat("00.00"));
        celda.setCellValue(turismo);

        //Celda de % funciona
        celda = fila.createCell((short) 15);
        celda.setCellStyle(cs3);
        double funciona = Integer.parseInt(rs.getString("estado_ve"));

        String estado_ve = (!rs.getString("estado_ve").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format((funciona / num_pedido) * 100).concat(" % ")
                : "0 % ";
        celda.setCellStyle(cs3);
        celda.setCellValue(estado_ve);

        //Celda % Gra Unitaria
        celda = fila.createCell((short) 16);
        celda.setCellStyle(cs3);
        double soporte = Integer.parseInt(rs.getString("soporte"));

        String grua = (!rs.getString("soporte").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format((soporte / num_pedido) * 100).concat(" % ")
                : "0 % ";

        celda.setCellStyle(cs3);
        celda.setCellValue(grua);

        //Celda del num. pedido
        celda = fila.createCell((short) 17);
        celda.setCellStyle(cs3);
        celda.setCellValue(num_pedido);

        //Se incrementa el numero de fila
        num_fila++;
        num_fila_aux++;
    }
}

From source file:neg.JRViewerInformeUnitario.java

License:Open Source License

private void btnExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcelActionPerformed

    String queryInf = this.jasperPrint.getProperty("query");

    try {//from  w w  w .j  av a  2 s  .c om
        // Se crea el libro excel
        HSSFWorkbook libro = new HSSFWorkbook();
        //Se crea la hoja
        HSSFSheet hoja = libro.createSheet("Informe");
        //Numero de fila de la hoja Excel
        int num_fila = 1;
        crearCabeceraHojaExcel(libro, hoja);

        HSSFCellStyle cs2 = libro.createCellStyle();
        cs2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs2.setBottomBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs2.setLeftBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs2.setRightBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs2.setTopBorderColor(HSSFColor.BLACK.index);

        HSSFCellStyle cs3 = libro.createCellStyle();
        cs3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs3.setBottomBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs3.setLeftBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs3.setRightBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs3.setTopBorderColor(HSSFColor.BLACK.index);
        ResultSet rs = CSDesktop.datos.select(queryInf);

        crearFilaHojaExcel(libro, hoja, num_fila, rs, cs2, cs3);
        FileOutputStream elFichero = null;
        elFichero = new FileOutputStream("c:\\informe_unitario.xls");
        libro.write(elFichero);
        elFichero.close();
        elFichero.flush();
        String property = "java.io.tmpdir";
        String tempDir = System.getProperty(property);
        System.out.println("OS current temporary directory is " + tempDir);
        String file = new String("C:\\informe_unitario.xls");
        Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file);

    } catch (SQLException ex) {
        Logger.getLogger(CSResultBuscarPedidoNew.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnknownHostException ex) {
        Logger.getLogger(CSResultBuscarPedidoNew.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CSResultBuscarPedidoNew.class.getName()).log(Level.SEVERE, null, ex);
    }

    //System.out.println("Vamoooooos: "+queryInf);

}

From source file:neg.JRViewerInformeUnitario.java

License:Open Source License

private static void crearCabeceraHojaExcel(HSSFWorkbook libro, HSSFSheet hoja) {
    HSSFRow fila = null;/*w ww .j  a v a  2 s.  co m*/
    HSSFCell celda = null;

    // Modificamos la fuente por defecto para que salga en negrita
    HSSFCellStyle cs = libro.createCellStyle();
    HSSFFont f = libro.createFont();
    f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    f.setColor(HSSFColor.WHITE.index);
    cs.setFont(f);
    //cs.setFillBackgroundColor(HSSFColor.GREEN.index);
    cs.setFillForegroundColor(HSSFColor.GREEN.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cs.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
    cs.setBottomBorderColor(HSSFColor.BLACK.index);
    cs.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
    cs.setLeftBorderColor(HSSFColor.BLACK.index);
    cs.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
    cs.setRightBorderColor(HSSFColor.BLACK.index);
    cs.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
    cs.setTopBorderColor(HSSFColor.BLACK.index);

    cs.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

    // Creamos la cabecera de las columnas
    fila = hoja.createRow(0);

    celda = fila.createCell((short) 0);
    celda.setCellStyle(cs);
    HSSFRichTextString texto = new HSSFRichTextString("DATOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 0, (short) ((60 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 1);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ENERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 1, (short) ((60 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 2);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("FEBRERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 2, (short) ((80 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 3);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MARZO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 3, (short) ((200 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 4);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ABRIL");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 4, (short) ((130 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 5);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MAYO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 5, (short) ((60 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 6);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JUNIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 6, (short) ((130 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 7);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JULIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 7, (short) ((60 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 8);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("AGOSTO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 8, (short) ((80 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 9);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("SEPTIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 9, (short) ((40 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 10);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("OCTUBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 10, (short) ((120 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 11);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NOVIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 11, (short) ((110 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 12);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("DICIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 12, (short) ((130 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 13);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("M.G.");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 13, (short) ((130 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 14);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("M.G. PEDIDO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 14, (short) ((130 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 15);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NUM.PEDIDOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 15, (short) ((40 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 16);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("IMPORTE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 16, (short) ((200 * 2) / ((double) 1 / 20)));

}

From source file:neg.JRViewerInformeUnitario.java

License:Open Source License

private static void crearFilaHojaExcel(HSSFWorkbook libro, HSSFSheet hoja, int num_fila, ResultSet rs,
        HSSFCellStyle cs2, HSSFCellStyle cs3) throws SQLException, UnknownHostException {
    HSSFRow fila = null;//  ww  w.  j a v  a 2  s .  co m
    HSSFCell celda = null;
    HSSFRichTextString texto = null;
    int num_fila_aux = 2;

    for (int i = 0; i < 4; i++) {
        //System.out.println();
        /*}
        while (rs.next())
        {*/
        if (i == 0) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);

            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("Facturacin");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de enero
            celda = fila.createCell((short) 1);
            String enero = rs.getString("enero");
            texto = new HSSFRichTextString(enero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de febrero
            celda = fila.createCell((short) 2);
            String febrero = rs.getString("febrero");
            texto = new HSSFRichTextString(febrero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de marzo
            celda = fila.createCell((short) 3);
            celda.setCellStyle(cs2);
            String marzo = rs.getString("marzo");
            texto = new HSSFRichTextString(marzo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de abril
            celda = fila.createCell((short) 4);
            String abril = rs.getString("abril");
            texto = new HSSFRichTextString(abril);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mayo
            celda = fila.createCell((short) 5);
            celda.setCellStyle(cs2);
            String mayo = rs.getString("mayo");
            texto = new HSSFRichTextString(mayo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de junio
            celda = fila.createCell((short) 6);
            String junio = rs.getString("junio");
            texto = new HSSFRichTextString(junio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del julio
            celda = fila.createCell((short) 7);
            String julio = rs.getString("julio");
            texto = new HSSFRichTextString(julio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de agosto
            celda = fila.createCell((short) 8);
            celda.setCellStyle(cs2);
            String agosto = rs.getString("agosto");
            texto = new HSSFRichTextString(agosto);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de septiembre
            celda = fila.createCell((short) 9);
            String septiembre = rs.getString("septiembre");
            texto = new HSSFRichTextString(septiembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de octubre
            celda = fila.createCell((short) 10);
            celda.setCellStyle(cs2);
            String octubre = rs.getString("octubre");
            texto = new HSSFRichTextString(octubre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de noviembre
            celda = fila.createCell((short) 11);
            String noviembre = rs.getString("noviembre");
            texto = new HSSFRichTextString(noviembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de diciembre
            celda = fila.createCell((short) 12);
            celda.setCellStyle(cs2);
            String diciembre = rs.getString("diciembre");
            texto = new HSSFRichTextString(diciembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

        } else if (i == 1) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);

            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("Margen pedido");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de enero
            celda = fila.createCell((short) 1);
            String enero = rs.getString("mg_pe_enero");
            texto = new HSSFRichTextString(enero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de febrero
            celda = fila.createCell((short) 2);
            String febrero = rs.getString("mg_pe_febrero");
            texto = new HSSFRichTextString(febrero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de marzo
            celda = fila.createCell((short) 3);
            celda.setCellStyle(cs2);
            String marzo = rs.getString("mg_pe_marzo");
            texto = new HSSFRichTextString(marzo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de abril
            celda = fila.createCell((short) 4);
            String abril = rs.getString("mg_pe_abril");
            texto = new HSSFRichTextString(abril);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mayo
            celda = fila.createCell((short) 5);
            celda.setCellStyle(cs2);
            String mayo = rs.getString("mg_pe_mayo");
            texto = new HSSFRichTextString(mayo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de junio
            celda = fila.createCell((short) 6);
            String junio = rs.getString("mg_pe_junio");
            texto = new HSSFRichTextString(junio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del julio
            celda = fila.createCell((short) 7);
            String julio = rs.getString("mg_pe_julio");
            texto = new HSSFRichTextString(julio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de agosto
            celda = fila.createCell((short) 8);
            celda.setCellStyle(cs2);
            String agosto = rs.getString("mg_pe_agosto");
            texto = new HSSFRichTextString(agosto);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de septiembre
            celda = fila.createCell((short) 9);
            String septiembre = rs.getString("mg_pe_septiembre");
            texto = new HSSFRichTextString(septiembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de octubre
            celda = fila.createCell((short) 10);
            celda.setCellStyle(cs2);
            String octubre = rs.getString("mg_pe_octubre");
            texto = new HSSFRichTextString(octubre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de noviembre
            celda = fila.createCell((short) 11);
            String noviembre = rs.getString("mg_pe_noviembre");
            texto = new HSSFRichTextString(noviembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de diciembre
            celda = fila.createCell((short) 12);
            celda.setCellStyle(cs2);
            String diciembre = rs.getString("mg_pe_diciembre");
            texto = new HSSFRichTextString(diciembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

        } else if (i == 2) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);

            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

        } else if (i == 3) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);
            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("NM. PEDIDOS");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de enero
            celda = fila.createCell((short) 1);
            String enero = rs.getString("num_enero");
            texto = new HSSFRichTextString(enero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de febrero
            celda = fila.createCell((short) 1);
            String febrero = rs.getString("num_febrero");
            texto = new HSSFRichTextString(febrero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de marzo
            celda = fila.createCell((short) 2);
            celda.setCellStyle(cs2);
            String marzo = rs.getString("num_marzo");
            texto = new HSSFRichTextString(marzo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de abril
            celda = fila.createCell((short) 3);
            String abril = rs.getString("num_abril");
            texto = new HSSFRichTextString(abril);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mayo
            celda = fila.createCell((short) 4);
            celda.setCellStyle(cs2);
            String mayo = rs.getString("num_mayo");
            texto = new HSSFRichTextString(mayo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de junio
            celda = fila.createCell((short) 5);
            String junio = rs.getString("num_junio");
            texto = new HSSFRichTextString(junio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del julio
            celda = fila.createCell((short) 6);
            String julio = rs.getString("num_julio");
            texto = new HSSFRichTextString(julio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de agosto
            celda = fila.createCell((short) 7);
            celda.setCellStyle(cs2);
            String agosto = rs.getString("num_agosto");
            texto = new HSSFRichTextString(agosto);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de septiembre
            celda = fila.createCell((short) 8);
            String septiembre = rs.getString("num_septiembre");
            texto = new HSSFRichTextString(septiembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de octubre
            celda = fila.createCell((short) 9);
            celda.setCellStyle(cs2);
            String octubre = rs.getString("num_octubre");
            texto = new HSSFRichTextString(octubre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de noviembre
            celda = fila.createCell((short) 10);
            String noviembre = rs.getString("num_noviembre");
            texto = new HSSFRichTextString(noviembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de diciembre
            celda = fila.createCell((short) 11);
            celda.setCellStyle(cs2);
            String diciembre = rs.getString("num_diciembre");
            texto = new HSSFRichTextString(diciembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de MG
            celda = fila.createCell((short) 12);
            double t_cliente = Double.parseDouble(rs.getString("ta_cliente"));
            double t_proveedor = Double.parseDouble(rs.getString("ta_proveedor"));

            String porCientoPedido = new java.text.DecimalFormat("#,##0")
                    .format(((t_cliente - t_proveedor) / t_cliente) * 100).concat("% ");
            texto = new HSSFRichTextString(porCientoPedido);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mg pedido
            celda = fila.createCell((short) 13);
            String mg_pedido = rs.getString("mg_pedido");
            texto = new HSSFRichTextString(mg_pedido);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del num. pedido
            celda = fila.createCell((short) 14);
            String num_pedido = rs.getString("num_pedido");
            texto = new HSSFRichTextString(num_pedido);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del ta cliente
            celda = fila.createCell((short) 15);
            String ta_cliente = rs.getString("ta_cliente");
            texto = new HSSFRichTextString(ta_cliente);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);
        }
        //Celda de Tarifa Cliente
        HSSFDataFormat format = libro.createDataFormat();
        HSSFCellStyle style = libro.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setTopBorderColor(HSSFColor.BLACK.index);

        style.setDataFormat(format.getFormat("00"));

        celda.setCellStyle(style);

        //Se incrementa el numero de fila
        num_fila++;
        num_fila_aux++;
    }
}

From source file:net.chaosserver.timelord.data.ExcelDataReaderWriter.java

License:Open Source License

/**
 * Builds a map of style name to HSSFCellStyle objects that can be used to
 * mark cells with similar styles./*w  w w. j  a  va  2 s . co  m*/
 *
 * @param wb the workbook needed to create the objects
 * @return the map of styles
 */
protected Map<String, HSSFCellStyle> buildStyleMap(HSSFWorkbook wb) {
    Map<String, HSSFCellStyle> styleMap = new HashMap<String, HSSFCellStyle>();

    HSSFCellStyle style;
    HSSFFont font;

    style = wb.createCellStyle();
    font = wb.createFont();
    // font.setItalic(true);
    font.setColor((short) 0xc); // blue
    style.setFont(font);
    styleMap.put("taskNoteStyle", style);

    style = wb.createCellStyle();
    font = wb.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    style.setFont(font);
    style.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleMap.put("taskNameHeaderStyle", style);

    style = wb.createCellStyle();
    style.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("taskNameStyle", style);

    style = wb.createCellStyle();
    style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
    style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    styleMap.put("topRowStyle", style);

    style = wb.createCellStyle();
    style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
    style.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("topLeftStyle", style);

    style = wb.createCellStyle();
    style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
    style.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("topRightStyle", style);

    style = wb.createCellStyle();
    font = wb.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    style.setFont(font);
    style.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    styleMap.put("totalHeaderStyle", style);

    style = wb.createCellStyle();
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("totalColumnStyle", style);

    style = wb.createCellStyle();
    style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setFont(font);
    styleMap.put("boldDateStyle", style);

    style = wb.createCellStyle();
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
    style.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("bottomLeftStyle", style);

    style = wb.createCellStyle();
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("bottomRightStyle", style);

    style = wb.createCellStyle();
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
    styleMap.put("bottomStyle", style);

    return styleMap;
}

From source file:net.mindengine.oculus.frontend.service.report.ExcelReport.java

License:Open Source License

public void writeDocument(SavedRun savedRun, List<TestRunSearchData> testRuns) throws IOException {
    HSSFWorkbook workBook = new HSSFWorkbook();

    HSSFSheet sheet = workBook.createSheet();
    HSSFRow headerRow = sheet.createRow(0);
    HSSFCellStyle columnHeaderStyle = workBook.createCellStyle();
    columnHeaderStyle.setBorderTop((short) 1);
    columnHeaderStyle.setBorderLeft((short) 1);
    columnHeaderStyle.setBorderRight((short) 1);
    columnHeaderStyle.setBorderBottom((short) 1);
    columnHeaderStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
    columnHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCell cell;// w  ww . j  a v a  2 s  .com

    cell = headerRow.createCell((short) 0);
    cell.setCellValue(new HSSFRichTextString("Log"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 1);
    cell.setCellValue(new HSSFRichTextString("Test Run Id"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 2);
    cell.setCellValue(new HSSFRichTextString("Test Name"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 3);
    cell.setCellValue(new HSSFRichTextString("Project"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 4);
    cell.setCellValue(new HSSFRichTextString("Status"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 5);
    cell.setCellValue(new HSSFRichTextString("Designer"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 6);
    cell.setCellValue(new HSSFRichTextString("Runner"));
    cell.setCellStyle(columnHeaderStyle);

    cell = headerRow.createCell((short) 7);
    cell.setCellValue(new HSSFRichTextString("Start Time"));
    cell.setCellStyle(columnHeaderStyle);

    int id = 0;

    HSSFCellStyle rowPassedStyle = workBook.createCellStyle();
    rowPassedStyle.setBorderTop((short) 1);
    rowPassedStyle.setBorderLeft((short) 1);
    rowPassedStyle.setBorderRight((short) 1);
    rowPassedStyle.setBorderBottom((short) 1);
    rowPassedStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    rowPassedStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    rowPassedStyle.setWrapText(true);

    HSSFCellStyle rowWarnStyle = workBook.createCellStyle();
    rowWarnStyle.setBorderTop((short) 1);
    rowWarnStyle.setBorderLeft((short) 1);
    rowWarnStyle.setBorderRight((short) 1);
    rowWarnStyle.setBorderBottom((short) 1);
    rowWarnStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
    rowWarnStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    rowWarnStyle.setWrapText(true);

    HSSFCellStyle rowFailStyle = workBook.createCellStyle();
    rowFailStyle.setBorderTop((short) 1);
    rowFailStyle.setBorderLeft((short) 1);
    rowFailStyle.setBorderRight((short) 1);
    rowFailStyle.setBorderBottom((short) 1);
    rowFailStyle.setFillForegroundColor(HSSFColor.RED.index);
    rowFailStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    rowFailStyle.setWrapText(true);

    HSSFCellStyle cellStyle = null;
    for (TestRunSearchData run : testRuns) {
        if (run.getTestRunStatus().equals("PASSED")) {
            cellStyle = rowPassedStyle;
        } else if (run.getTestRunStatus().equals("FAILED")) {
            cellStyle = rowFailStyle;
        } else
            cellStyle = rowWarnStyle;
        id++;
        HSSFRow row = sheet.createRow(id);

        cell = row.createCell((short) 0);
        cell.setCellValue(new HSSFRichTextString(
                "http://" + config.getOculusServerUrl() + "/report/report-" + run.getTestRunId()));
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 1);
        cell.setCellValue(run.getTestRunId());
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 2);
        cell.setCellValue(new HSSFRichTextString(run.getFetchTestName()));
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 3);
        cell.setCellValue(new HSSFRichTextString(run.getFetchProjectName()));
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 4);
        cell.setCellValue(new HSSFRichTextString(run.getTestRunStatus()));
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 5);
        cell.setCellValue(new HSSFRichTextString(run.getDesignerName()));
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 6);
        cell.setCellValue(new HSSFRichTextString(run.getRunnerName()));
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 7);
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy  HH:mm:ss");
        cell.setCellValue(new HSSFRichTextString(sdf.format(run.getTestRunStartTime())));
        cell.setCellStyle(cellStyle);

    }

    workBook.write(outputStream);

}

From source file:net.mindengine.oculus.frontend.service.report.ExcelTestSearchReportGenerator.java

License:Open Source License

/**
 * Generates Excel spreadsheet./*from www  .java  2  s . c om*/
 *
 * @param searchResult Search result for test or document (test-case)
 * @throws Exception 
 */
public void writeExcelReports(BrowseResult<?> searchResult, Long projectId, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    int cellOffset = 5;
    /*
     * Customizations which will be exported to XLS spreadsheet.
     */
    Collection<Customization> customizationsExport = new LinkedList<Customization>();

    /*
     * Here will be cell ids stored for each customization. This is needed because there might be a lot of merged cells for list customizations
     */
    Map<Long, Integer> customizationCells = new HashMap<Long, Integer>();

    if (projectId != null && projectId > 0) {
        Collection<Customization> customizations = customizationDAO.getCustomizations(projectId, unit);
        for (Customization customization : customizations) {

            //Checking if the user has selected this specific customization for exporting
            if ("on".equals(request.getParameter("cexport" + customization.getId()))) {

                customizationCells.put(customization.getId(), cellOffset);

                //Checking if the customization contains possible values and fetching them all
                if (customization.getType().equals(Customization.TYPE_CHECKLIST)
                        || customization.getType().equals(Customization.TYPE_LIST)) {
                    customization.setPossibleValues(
                            customizationDAO.getCustomizationPossibleValues(customization.getId()));
                    cellOffset += customization.getPossibleValues().size();
                } else {
                    cellOffset += 1;
                }

                customizationsExport.add(customization);
            }
        }
    }

    /*
     * Generating the Excel spreadsheet
     */

    OutputStream outputStream = response.getOutputStream();
    response.setContentType("application/ms-excel");

    HSSFWorkbook workBook = new HSSFWorkbook();

    HSSFSheet sheet = workBook.createSheet();

    HSSFFont fontHeader = workBook.createFont();
    fontHeader.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontHeader.setColor(HSSFColor.WHITE.index);

    HSSFCellStyle columnHeaderStyle = workBook.createCellStyle();
    columnHeaderStyle.setBorderTop((short) 2);
    columnHeaderStyle.setBorderLeft((short) 2);
    columnHeaderStyle.setBorderRight((short) 2);
    columnHeaderStyle.setBorderBottom((short) 2);
    columnHeaderStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
    columnHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    columnHeaderStyle.setFont(fontHeader);

    HSSFCellStyle columnRootHeaderStyle = workBook.createCellStyle();

    //columnRootHeaderStyle.cloneStyleFrom(columnHeaderStyle);
    columnRootHeaderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFCell cell;

    HSSFRow headerRow = sheet.createRow(0);
    HSSFRow header2Row = sheet.createRow(1);

    sheet.addMergedRegion(new Region((short) 0, (short) 0, (short) 0, (short) 4));
    cell = headerRow.createCell((short) 0);
    cell.setCellValue("Common");
    cell.setCellStyle(columnRootHeaderStyle);

    cell = header2Row.createCell((short) 0);
    cell.setCellValue("Test");
    cell.setCellStyle(columnHeaderStyle);

    cell = header2Row.createCell((short) 1);
    cell.setCellValue("Project");
    cell.setCellStyle(columnHeaderStyle);

    cell = header2Row.createCell((short) 2);
    cell.setCellValue("Sub-Project");
    cell.setCellStyle(columnHeaderStyle);

    cell = header2Row.createCell((short) 3);
    cell.setCellValue("Author");
    cell.setCellStyle(columnHeaderStyle);

    cell = header2Row.createCell((short) 4);
    cell.setCellValue("Created");
    cell.setCellStyle(columnHeaderStyle);

    for (Customization customization : customizationsExport) {
        int cellId = customizationCells.get(customization.getId());
        if (customization.getPossibleValues() != null) {
            int size = customization.getPossibleValues().size();
            if (size > 1) {
                sheet.addMergedRegion(
                        new Region((short) 0, (short) 0, (short) cellId, (short) (cellId + size - 1)));
            }

            /*
             * Exporting possible values 
             */
            int offset = 0;
            for (CustomizationPossibleValue cpv : customization.getPossibleValues()) {
                cell = header2Row.createCell((short) (cellId + offset));
                cell.setCellValue(cpv.getPossibleValue());
                cell.setCellStyle(columnHeaderStyle);
                offset++;
            }
        } else {
            cell = header2Row.createCell((short) cellId);
            cell.setCellStyle(columnHeaderStyle);
        }
        cell = headerRow.createCell((short) cellId);
        cell.setCellValue(customization.getName());
        cell.setCellStyle(columnRootHeaderStyle);
    }

    HSSFCellStyle cellStyle = workBook.createCellStyle();

    HSSFCellStyle checkboxStyle = workBook.createCellStyle();
    checkboxStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
    checkboxStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    checkboxStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontCheckbox = workBook.createFont();
    fontCheckbox.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontCheckbox.setColor(HSSFColor.BLACK.index);
    checkboxStyle.setFont(fontCheckbox);

    HSSFCellStyle boolYesStyle = workBook.createCellStyle();
    boolYesStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    boolYesStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    HSSFCellStyle boolNoStyle = workBook.createCellStyle();
    boolNoStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
    boolNoStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    /*
     * Used in order to cache user names. Key = ID, Value = User name
     */
    Map<Long, String> usersCache = new HashMap<Long, String>();

    int j = 2;
    for (Object object : searchResult.getResults()) {
        HSSFRow row = sheet.createRow(j);

        String name, parentProjectName, projectName, authorName;
        Date date;
        Long objectId;

        if (object instanceof Test) {
            Test test = (Test) object;
            name = test.getName();
            parentProjectName = test.getParentProjectName();
            projectName = test.getProjectName();
            authorName = test.getAuthorName();
            objectId = test.getId();
            date = test.getDate();
        } else
            throw new IllegalArgumentException(object.getClass().getName());

        cell = row.createCell((short) 0);
        cell.setCellValue(name);
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 1);
        cell.setCellValue(parentProjectName);
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 2);
        cell.setCellValue(projectName);
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 3);
        cell.setCellValue(authorName);
        cell.setCellStyle(cellStyle);

        cell = row.createCell((short) 4);
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        cell.setCellValue(sdf.format(date));
        cell.setCellStyle(cellStyle);

        int offset = 5;

        for (Customization customization : customizationsExport) {
            UnitCustomizationValue ucv = customizationDAO.getUnitCustomizationValue(customization.getId(),
                    objectId);

            if (customization.getType().equals(Customization.TYPE_CHECKLIST)
                    || customization.getType().equals(Customization.TYPE_LIST)) {
                for (CustomizationPossibleValue cpv : customization.getPossibleValues()) {

                    boolean matches = false;

                    if (customization.getType().equals(Customization.TYPE_LIST)) {
                        if (ucv != null && ucv.getValue() != null && ucv.getValue().equals("" + cpv.getId())) {
                            matches = true;
                        }
                    } else {
                        String s = "(" + cpv.getId() + ")";
                        if (ucv != null && ucv.getValue() != null && ucv.getValue().contains(s)) {
                            matches = true;
                        }
                    }

                    if (matches) {
                        cell = row.createCell((short) offset);
                        cell.setCellValue("X");
                        cell.setCellStyle(checkboxStyle);
                    }
                    offset++;
                }
            } else {
                if (ucv != null) {
                    cell = row.createCell((short) offset);
                    cell.setCellStyle(cellStyle);

                    if (customization.getType().equals(Customization.TYPE_ASSIGNEE)) {
                        if (ucv.getValue() != null && !ucv.getValue().isEmpty()) {
                            try {
                                Long userId = Long.parseLong(ucv.getValue());
                                /*
                                 * Chaching user names by their ids
                                 */
                                String userName = null;
                                if (!usersCache.containsKey(userId)) {
                                    User user = userDAO.getUserById(userId);
                                    if (user != null) {
                                        userName = user.getName();
                                    } else
                                        userName = "";

                                    usersCache.put(userId, userName);
                                } else
                                    userName = usersCache.get(userId);

                                cell.setCellValue(userName);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else if (customization.getType().equals(Customization.TYPE_CHECKBOX)) {
                        if (ucv.getValue() != null) {
                            if (ucv.getValue().equals("true")) {
                                cell.setCellValue("Yes");
                                cell.setCellStyle(boolYesStyle);
                            } else {
                                cell.setCellValue("No");
                                cell.setCellStyle(boolNoStyle);
                            }
                        }
                    } else {
                        cell.setCellValue(ucv.getValue());
                    }

                }
                offset++;
            }
        }
        j++;
    }

    /*
     * Making the text to fit in all cells 
     */
    for (short i = 0; i < (short) cellOffset + 1; i++) {
        sheet.autoSizeColumn(i);
    }

    workBook.write(outputStream);
    outputStream.flush();
    outputStream.close();
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

protected HSSFCellStyle getLoadedCellStyle(StyleInfo style) {
    HSSFCellStyle cellStyle = loadedCellStyles.get(style);
    if (cellStyle == null) {
        cellStyle = workbook.createCellStyle();

        cellStyle.setFillForegroundColor(style.backcolor);
        cellStyle.setFillPattern(style.mode);
        cellStyle.setAlignment(style.horizontalAlignment);
        cellStyle.setVerticalAlignment(style.verticalAlignment);
        cellStyle.setRotation(style.rotation);
        cellStyle.setFont(style.font);//from  w w  w. ja  v  a  2 s . c  om
        cellStyle.setWrapText(style.lcWrapText);
        cellStyle.setLocked(style.lcCellLocked);
        cellStyle.setHidden(style.lcCellHidden);

        if (style.hasDataFormat()) {
            cellStyle.setDataFormat(style.getDataFormat());
        }

        boolean isIgnoreCellBorder = getCurrentItemConfiguration().isIgnoreCellBorder();
        if (!isIgnoreCellBorder) {
            BoxStyle box = style.box;
            cellStyle.setBorderTop(box.borderStyle[BoxStyle.TOP]);
            cellStyle.setTopBorderColor(box.borderColour[BoxStyle.TOP]);
            cellStyle.setBorderLeft(box.borderStyle[BoxStyle.LEFT]);
            cellStyle.setLeftBorderColor(box.borderColour[BoxStyle.LEFT]);
            cellStyle.setBorderBottom(box.borderStyle[BoxStyle.BOTTOM]);
            cellStyle.setBottomBorderColor(box.borderColour[BoxStyle.BOTTOM]);
            cellStyle.setBorderRight(box.borderStyle[BoxStyle.RIGHT]);
            cellStyle.setRightBorderColor(box.borderColour[BoxStyle.RIGHT]);
        }

        loadedCellStyles.put(style, cellStyle);
    }
    return cellStyle;
}