Search code examples
javaapache-poi

POI API - Reference to font by the path


Am using POI API to generate excel reports and i have a font file

What i need is to use this font while generating the report, and while searching in the API functions , i didn't find any function that take the path of the font as a parameter.

only the following function supported:

createFont()
getFontAt(short idx)
findFont(short boldWeight,
            short color,
            short fontHeight,
            java.lang.String name,
            boolean italic,
            boolean strikeout,
            short typeOffset,
            byte underline)

Could you please advise, How can i do that ?


Solution

  • You should use HSSFFont for that. Take a look at the API.

    For example:

    public class ChangeCellFontName {
        public static void main(String[] args) throws Exception {
    
            /* Create Workbook and Worksheet */
            HSSFWorkbook my_workbook = new HSSFWorkbook();
            HSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
    
            /* Get access to HSSFCellStyle */
            HSSFCellStyle my_style = my_workbook.createCellStyle();
    
            /* Create HSSFFont object from the workbook */
            HSSFFont my_font = my_workbook.createFont();
    
            /* Set the font name to Verdana */
            my_font.setFontName("Verdana");
    
            /* Also make the font color to RED */
            my_font.setColor(HSSFFont.COLOR_RED);
    
            /* attach the font to the style created earlier */
            my_style.setFont(my_font);
    
            /* Attach the new font 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("The font for this text would be Verdana");
    
            /* Attach the style to the cell */
            cell.setCellStyle(my_style);
            
        }
    }
    

    This last example was taken from here. Also, another example.