15
votes

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?

3
I have seen posts like these where the row is made bold, but this is just for one column - thinktibits.blogspot.com/2012/12/…. I have close to 6 columns in my row.rickygrimes

3 Answers

31
votes

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.

3
votes

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);
    }
}
1
votes
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class test {
public static void main(String[] args) throws FileNotFoundException, IOException {
    String toCreateFullPath="C:\\Users\\Y[![enter image description here][1]][1]our Desired Path\\testExcel.xlsx";
    Path path=Paths.get(toCreateFullPath);
    if(!Files.exists(path.getParent())){

        try {
            Files.createDirectory(path.getParent());
            System.out.println("Directory created");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Error Directory is Not Created");
        }
    }

    XSSFWorkbook workbook =new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("testSheet");
    String[] columns = {"id", "Name", "Family","Address", "Phone"};
    CellStyle hStyle=null;
    // Creating a font
    XSSFFont font= workbook.createFont();
    font.setFontHeightInPoints((short)12);
    font.setFontName("Arial");
    font.setColor(IndexedColors.WHITE.getIndex());
    font.setBold(true);
    font.setItalic(false);

    hStyle=workbook.createCellStyle();
    hStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    hStyle.setAlignment(CellStyle.ALIGN_CENTER);
    // Setting font to style
    hStyle.setFont(font);


    // Create a Row
    Row headerRow = sheet.createRow(0);
    // Create cells
    for(int i = 0; i < columns.length; i++) {
        Cell cell = headerRow.createCell(i);
        cell.setCellValue(columns[i]);
        // Setting cell style
        cell.setCellStyle(hStyle);  
        sheet.autoSizeColumn(i);
    }

    try (FileOutputStream outputStream = new FileOutputStream(toCreateFullPath)) {

        workbook.write(outputStream);

    }


}

}

finally the result would be : enter image description here