Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the entire row bold using Apache POI

I am using Apache POI's HSSFWorkbook to write data to Excel spreadsheets.

I want to make an entire row bold. Can someone please suggest how to do it?

like image 701
rickygrimes Avatar asked Aug 21 '14 23:08

rickygrimes


People also ask

How do I use bold text style for an entire row using Apache POI?

createRow((int)0); CellStyle style=headRow. getRowStyle(); Font boldFont=hwb. createFont(); boldFont. setBoldweight(Font.

How do you set bold in Java?

To make a text bold create a font bypassing FontWeight. BOLD or, FontWeight. EXTRA_BOLD as the value of the parameter weight and, to make a text italic pass FontPosture. ITALIC as the value of the parameter posture.

How do I change font size in Apache POI?

the POI HSSF Font class has two font size settings methods: setFontHeight(short) - Set the font height in unit's of 1/20th of a point. setFontHeightInPoints(short) - Set the font height in point.


2 Answers

Would something like this work with what you have:

public static void makeRowBold(Workbook wb, Row row){
    CellStyle style = wb.createCellStyle();//Create style
    Font font = wb.createFont();//Create font
    font.setBold(true);//Make font bold
    style.setFont(font);//set it to bold

    for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
        row.getCell(i).setCellStyle(style);//Set the style
    }
}

It basically goes over each cell in the row passed in, setting the style to a bold one. Should result in the whole row being set to the desired style.

Good Luck!

EDIT

A more complete example:

public static void main(String[] args) {
    Path myFile = Paths.get(System.getProperty("user.home"), "Desktop", "tester.xlsx");

        try {
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(myFile.toFile()));
            XSSFSheet sheet = wb.getSheetAt(0);
            makeRowBold(wb, sheet.getRow(0));

            wb.write(new FileOutputStream(myFile.toFile()));
        } catch (IOException e) {
            e.printStackTrace();
        }
}


public static void makeRowBold(Workbook wb, Row row){
    CellStyle style = wb.createCellStyle();//Create style
    Font font = wb.createFont();//Create font
    font.setBold(true);//Make font bold
    style.setFont(font);//set it to bold

    for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
        row.getCell(i).setCellStyle(style);//Set the sty;e
    }
}

This was tested on an xlsx file with data in row 1, the resulting file had bold data afterwards.

like image 172
Levenal Avatar answered Oct 02 '22 12:10

Levenal


Using the non-deprecated API:

public static void makeBold(Workbook workbook, Row row)
{
    CellStyle cellStyle = cell.getCellStyle();
    cellStyle.setFont(font);

    for (int rowIndex = 0; rowIndex < row.getLastCellNum(); rowIndex++)
    {
        Cell cell = row.getCell(rowIndex);
        XSSFFont font = (XSSFFont) workbook.createFont();
        font.setBold(true);
        cell.setCellStyle(cellStyle);
    }
}
like image 22
BullyWiiPlaza Avatar answered Oct 02 '22 13:10

BullyWiiPlaza