Excel POI cell in bold

-2

Good that such They know what is the method to put the text of a cell in black to apply the style from java with the class poi Thanks and a greeting.

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);

this is my code and I wanted to put the format in the headers

    
asked by zekamodz 04.06.2018 в 14:06
source

2 answers

0

I share a method that I generated in which you can define several properties for a cell, what you do in

/**
 * @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 == true ? true : false));
    cellStyle.setFont(headersFont);
    if (horizontalAlign != null) {
        cellStyle.setAlignment(horizontalAlign);
    }
    if (verticalAlign != null) {
        cellStyle.setVerticalAlignment(verticalAlign);
    }
    cellStyle = (backGround == true) ? setCellBackground(cellStyle, color) : cellStyle;
    cellStyle = (border == true) ? setCellBoder(cellStyle) : cellStyle;

    return cellStyle;
}

and the way to use it is as follows:

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

greetings

    
answered by 27.09.2018 / 01:53
source
0

You have to create a style:

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

And then apply it to a cell that requires it:

cell.setCellStyle(cellStyle);
    
answered by 04.06.2018 в 15:38