Fonts in Excel using Apache POI

Working in Excel files is made easy by the Apache POI library. Even more easy is the using and working with various fonts in excel using Apache POI.
We saw how we can Write Excel using Apache POI in the previous post.
Now we will see how we can explore the various functions that POI has provided to play with the Fonts.

To set font to the excel, we need to follow the below steps

1. Create a new Font, using the workbook

2. Alter the Font as per your needs.
you can set height, font name, italic and even strike it out

 3. Create a CellStyle into which assign the font created.

 4. Now use the CellStyle object and set to the cells that you want to apply the font.

Please see the complete code below


Fonts in Excel using Apache POI

Please Note

Their is a limit for creating CellStyles in a workbook. If you exceed the limit of 64000 CellStyles you may get the below exception

To avoid this exception you can create your font and CellStyle one time and then reuse it.
If you have code like this

You can easily change it to below code for better use of CellStyle and Fonts. This way you can write 10000 rows and cells with only 1 Font and CellStyle.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn