Java检查单元格中的文本是否使用Apache POI HSSF进行粗体显示

时间:2016-05-22 21:21:51

标签: java poi-hssf hssf

使用Java和Hssf获取Excel数据,如何检查单元格是否加粗? 我正在迭代Excel工作簿的所有工作表,所有行和所有单元格。如果单元格是粗体,我想执行一个动作。

//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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好的,这就是你如何使用它:

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