How to make an entire excel row cells bold text using Apache POI?
E.g:
Column headings should be in bold. Instead of applying style for each and every cell of heading row, how can I apply some style to an entire row?
createRow((int)0); CellStyle style=headRow. getRowStyle(); Font boldFont=hwb. createFont(); boldFont. setBoldweight(Font.
Apache POI provides three methods for changing the background color. In the CellStyle class, we can use the setFillForegroundColor, setFillPattern, and setFillBackgroundColor methods for this purpose. A list of colors is defined in the IndexedColors class. Similarly, a list of patterns is defined in FillPatternType.
This should work fine.
Workbook wb = new XSSFWorkbook("myWorkbook.xlsx"); Row row=sheet.getRow(0); CellStyle style=null; XSSFFont defaultFont= wb.createFont(); defaultFont.setFontHeightInPoints((short)10); defaultFont.setFontName("Arial"); defaultFont.setColor(IndexedColors.BLACK.getIndex()); defaultFont.setBold(false); defaultFont.setItalic(false); XSSFFont font= wb.createFont(); font.setFontHeightInPoints((short)10); font.setFontName("Arial"); font.setColor(IndexedColors.WHITE.getIndex()); font.setBold(true); font.setItalic(false); style=row.getRowStyle(); style.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFont(font);
If you do not create defaultFont
all your workbook will be using the other one as default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With