0

Buenas que tal Saben cual es el método para poner el texto de una celda en negro aplicar el estilo desde java con la clase poi Gracias y un saludo.

HSSFWorkbook a = archivoexcel();
    HSSFSheet hoja = a.createSheet("LOGS");
    int longitud = datos.size();
    Iterator ithora = hora.iterator();
    Iterator itnom= nom.iterator();
    Iterator itmin = min.iterator();
    Iterator itentorn = entorno.iterator();
    Iterator it = datos.iterator();
    while (it.hasNext()) {
        for (int i = 0; i < longitud; i++) {

            HSSFRow filacabe = hoja.createRow(0);
            HSSFCell celdaca = filacabe.createCell((short) 0);
            HSSFCell celda2ca = filacabe.createCell((short) 1);
            HSSFCell celda4ca = filacabe.createCell((short) 2);
            HSSFCell celda5ca = filacabe.createCell((short) 3);
            HSSFCell celda6ca = filacabe.createCell((short) 4);
            HSSFCell celda7ca = filacabe.createCell((short) 4);
            celdaca.setCellValue("Nombre test");
            celda2ca.setCellValue("MENSAJE");
            celda4ca.setCellValue("Entorno");
            celda5ca.setCellValue("Hora");
            celda6ca.setCellValue("FECHA");
            celda7ca.setCellValue("Nombre");




            HSSFRow fila = hoja.createRow(i + 1);
            HSSFCell celda = fila.createCell((short) 0);
            HSSFCell celda2 = fila.createCell((short) 1);
            HSSFCell celda4 = fila.createCell((short) 2);
            HSSFCell celda5 = fila.createCell((short) 3);
            HSSFCell celda6 = fila.createCell((short) 4);
            HSSFCell celda7 = fila.createCell((short) 4);
            String info = it.next().toString();
            String[] infoslip = info.split(";");

            celda.setCellValue(infoslip[0]);
            if (infoslip[1].equalsIgnoreCase("")) {

                celda2.setCellValue(infoslip[1]);
                celda4.setCellValue(itentorn.next().toString());
                celda5.setCellValue(ithora.next().toString()+": "+itmin.next().toString());
                celda6.setCellValue(fecha);
                celda7.setCellValue(itnom.next().toString());
            } else {
                celda2.setCellValue(infoslip[1]);
                celda4.setCellValue(itentorn.next().toString());
                celda5.setCellValue(ithora.next().toString()+": "+itmin.next().toString());
                celda6.setCellValue(fecha);
                celda7.setCellValue(itnom.next().toString());

            }

        }
    }
    this.archivo(a);

este es mi código y quería poner en las cabeceras el formato

2 respuestas 2

2

Tienes que crear un estilo:

CellStyle cellStyle = book.createCellStyle();
Font cellFont = book.createFont();
cellFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle.setFont(cellFont);

Y luego aplicarlo a celda que lo requiera:

cell.setCellStyle(cellStyle);
1

te comparto un método que generé en el cual puedes definir varias propiedades para una celda.

/**
 * @param: book:
 *             libro de trabajo de excel
 * @param: letterSize:
 *             Tamaño de letra
 * @param: color:
 *             valor del nuevo color
 * @param: isBold:
 *             define si el texto será en negritas
 * @param: border:
 *             define si la celda tendrá borde
 * @param: backGround:
 *             define si la celda tedrá fondo
 * @param: horizontalAlign:
 *             tipo de justificado horizontal
 * @param: verticalAlign:
 *             tipo de justificado vertical
 */
protected CellStyle setCellStyle(Workbook book, short fontsize, Short color, boolean isBold, boolean border,
        boolean backGround, HorizontalAlignment horizontalAlign, VerticalAlignment verticalAlign) {

    CellStyle cellStyle = book.createCellStyle();
    Font headersFont = book.createFont();

    headersFont.setFontHeightInPoints(fontsize);
    headersFont.setColor(IndexedColors.BLACK.getIndex());
    headersFont.setBold((isBold));
    cellStyle.setFont(headersFont);
    if (horizontalAlign != null) {
        cellStyle.setAlignment(horizontalAlign);
    }
    if (verticalAlign != null) {
        cellStyle.setVerticalAlignment(verticalAlign);
    }
    cellStyle = (backGround) ? setCellBackground(cellStyle, color) : cellStyle;
    cellStyle = (border) ? setCellBoder(cellStyle) : cellStyle;

    return cellStyle;
}

y la forma de utilizarlo es la siguiente:

CellStyle titleStyle = setCellStyle(book, (short) 14, IndexedColors.WHITE.getIndex(), true, false, false,
            HorizontalAlignment.LEFT, null);

saludos

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.