1

Using Java and Hssf to get Excel data, how to check if a cell is bolded? I am iterating through all sheets, all rows, and all cells of an Excel workbook. I want to perform an action if the cell is bolded.

//Iterate through excel sheets    
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
    HSSFSheet excelSheet = workbook.getSheetAt(i);
    HSSFRow row;
    Iterator rows = excelSheet.rowIterator();

    //Iterate through rows        
    while (rows.hasNext()) {
        row = (HSSFRow) rows.next();
        HSSFCell cell;
        Iterator cells = row.cellIterator();

        //Iterate through cells            
        while (cells.hasNext()) {
            cell = (HSSFCell) cells.next();

            //If cell contains all bolded text
            //DO Something
        }
    }
}
pseudorandom
  • 267
  • 5
  • 20

1 Answers1

1

Ok, so this is how you could use it:

//Iterate through cells            
  while (cells.hasNext()) {
        cell = (HSSFCell) cells.next();
        HSSFCellStyle style = (HSSFCellStyle) cell.getCellStyle();
        HSSFFont font = style.getFont(workbook);
        boolean isBold = font.getBold();
        System.out.println("Cell value : " + cell + ". Is bold? - " + isBold);
  }
Yev
  • 321
  • 3
  • 9