How to Format Excel Cell Bold -Java POI - Example Program

Format Excel Documents Using POI - Introduction


We are going to start a new set of tutorials from this post, that explains how to format excel workbooks in Java, using Apache POI library. I'm planning to divide this into multiple sets, as the number of options for applying styles to your POI workbooks is vast. Let us focus on some simple formatting techniques / options to start with and then move towards some advanced examples slowly. The tutorial is specific to formatting .XLS workbooks. We will cover .XLSX in a separate post.

The simplest formatting example we can provide to get started, is the one that explains how to make the cell content in bold. So, here we go. 

HSSFCellStyle


There exists a method "setCellStyle" in the class Cell, that takes an object of type HSSFCellStyle , which we can use for formatting. You can do anything with HSSFCellStyle, and we are going to see how to make cell bold with HSSFCellStyle. Let us get started.

Create a workbook and worksheet to start with. We have seen this quite a lot of times. A sample code is provided below:
                /* Create Workbook and Worksheet */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Bold Style example");

ok, we got a workbook and worksheet. What next?

We need to create a HSSFCellStyle, and define a style for it. HSSFWorkbook class has a method "createCellStyle" which returns an object of type HSSFCellStyle. There is an interesting note about this method in Apache documentation. POI allows you to set upto 4000 unique styles in a .xls workbook!. Here is the code that creates a blank HSSFCellStyle object.
                /* Get access to HSSFCellStyle */
                HSSFCellStyle my_style = my_workbook.createCellStyle();

HSSFFont to set Font Bold


HSSFCellStyle class has a method, setFont, which takes an object of type HSSFFont. You can use the method setBoldweight in HSSFFont to make the font bold.All fonts should be attached to the workbook. So, we get a HSSFFont from the HSSFWorkbook itself. Refer code below:
                /* Create HSSFFont object from the workbook */
                HSSFFont my_font=my_workbook.createFont();
                /* set the weight of the font */
                my_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                /* attach the font to the style created earlier */
                my_style.setFont(my_font);
                /* At this stage, we have a bold style created which we can attach to a cell */

Apply Bold Style to a Cell


All good so far. To test the style we created, we define a row and cell and attach the style to it using "setCellStyle" method. Here is how to do this:
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("This text will be in bold");
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);

Make a Cell Bold - Complete Java Program Example


It may look like lot of coding to make a cell bold in POI. But, once you understand the basics, it is very easy to do this. The complete Java program to make a Cell Bold in Java using Apache POI, is given below:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class makeCellBold {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Bold Style example");
                /* Get access to HSSFCellStyle */
                HSSFCellStyle my_style = my_workbook.createCellStyle();
                /* Create HSSFFont object from the workbook */
                HSSFFont my_font=my_workbook.createFont();
                /* set the weight of the font */
                my_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                /* attach the font to the style created earlier */
                my_style.setFont(my_font);
                /* At this stage, we have a bold style created which we can attach to a cell */
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("This text will be in bold");
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\bold_workbook.xls"));
                my_workbook.write(out);
                out.close();
                
                
        }
}
This program creates a cell and makes it bold, which is evident from the output screenshot below:

Excel cell made bold in Java using Apache POI
Excel cell made bold in Java using Apache POI
See you in a different formatting tutorial next time.

No comments:

Post a Comment