Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select and bold the whole worksheet with Apache POI

I am a beginner with Apache POI library.

in VBA, I know I can select and bold the whole worksheet with following code

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
ws.Cells.Font.Bold = True

May I know how to select and bold the whole sheet by coding with Apache POI library?

thanks

like image 828
mememoremore Avatar asked Mar 28 '11 22:03

mememoremore


People also ask

How do I use bold text style for an entire row using Apache POI?

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

How do I change the font style in Apache POI?

the POI HSSF Font class has two font size settings methods: setFontHeight(short) - Set the font height in unit's of 1/20th of a point. setFontHeightInPoints(short) - Set the font height in point.

How do I change cell format in Apache POI?

To create date cell which display current date to the cell, we can use Apache POI's style feature to format the cell according to the date. The setDateformat() method is used to set date format. Lets see an example in which we are creating a cell containing current date.


2 Answers

There is a pretty good example on this link.

Sheet sheet = wb.createSheet("test");
CellStyle cs = wb.createCellStyle();
Font f = wb.createFont();
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
cs.setFont(f);
sheet.setDefaultColumnStyle(1,cs); //set bold for column 1
like image 159
CoolBeans Avatar answered Sep 27 '22 20:09

CoolBeans


The default font for a workbook can be retrieved from index 0. So to modify the font bold setting default for the workbook:

private void setWorkbookDefaultFontToBold(Workbook workbook){
    Font defaultFont = workbook.getFontAt(0);
    defaultFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
}

It's a really obscure piece of information - it's in the POI Sheet Javadoc for setColumnWidth, in the second or so line:

"...can be displayed in a cell that is formatted with the standard font (first font in the workbook)."

I haven't had to use it heavily, so it may have just happened to work for me (the location and non-prevalence of documentation on it makes me slightly leary of recommending depending on it) but it's somewhere you could start looking

like image 20
romeara Avatar answered Sep 27 '22 19:09

romeara