Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply bold text style for an entire row using Apache POI?

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?

like image 206
Krishnamachary Avatar asked Sep 05 '12 17:09

Krishnamachary


People also ask

How do I make text bold in Apache POI?

createRow((int)0); CellStyle style=headRow. getRowStyle(); Font boldFont=hwb. createFont(); boldFont. setBoldweight(Font.

How do I change the background color of a cell in Apache POI?

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.


1 Answers

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.

like image 191
ArtiBucco Avatar answered Sep 24 '22 08:09

ArtiBucco