Make the entire row bold using Apache POI

rickygrimes :

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?

Levenal :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make JXTreeTable Entire Row Bold

Is there any way to make text both bold as well as italic in ppt using apache poi?

How to set bold font using the latest apache poi?

Setting a part of the cell contents to bold using apache poi?

apache POI: dataValidation (or style) for entire column, except for the header row?

Heading to be bold but non heading should be non bold in ppt textbox using apache poi hslf

Insert a Row in Excel Using Java Apache POI

How to remove a row using apache poi

How to select and bold the whole worksheet with Apache POI

Apache POI XWPFRun isBold does not detect bold

Bold the entire row based on a cell's font

Getting a nullPointerException in getting getLastCellNum() method in a blank Row, using apache poi

delete Excel row containing specific text using Apache_poi

Printing values in different column same row using APACHE POI

How can I make entire row submitable using jquery?

How to add Data validation to entire column of an excel sheet using apache poi in java?

Using Apache poi

Using for loop in apache poi

How to partially bold a Paragraph in Apache POI? (Word Documents)

How can i make a specific row bold?

Make an entire row a table clickable

Using Excel templates with Apache POI

Writing äöü to Word using Apache POI

how to get last column value of respective row of excel sheet in java using Apache POI

Unable to delete row using Apache POI because the cell contains email address

Not able to check in excel using Apache POI if specific cell as per row and column is empty in Selenium with Java

Read data from xlsx using JAVA apache poi fails to read the last cell of a row

How to decrease default height of a table row in word using apache poi in java

How to split a excel file into multiple files based on row count using apache poi?