Reading:  

Quick introduction to Apache POI


Working with fonts

This chapter shows how to set apply styles, different fonts and display text in different direction and angles.

Working with Fonts in Apache POI

In this chapter we will cover applying font and related styles to cell values.

The following code shows how to apply a font and style to cell contents

package com.sc;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

/**
* Created by SubjectCoach.com on 3/12/2015.
* POI version 3.11
*/
public class FontStylesExample {
public static void main(String[] args) {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("FontStyles");

// Let's create our first row
Row row = sheet.createRow(1);

// Create a new font and alter it.
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)19);
font.setFontName("Arial Black");
// make our font italic
font.setItalic(true);
// how about a line through
font.setStrikeout(true);
// color red
font.setColor((short) 2);
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
// apply font to cell style
style.setFont(font);

// Create a cell and put a value in it.
Cell cell = row.createCell(1);
cell.setCellValue("Font Style testing");
cell.setCellStyle(style);

try {
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:/poi/fontexample.xlsx");
wb.write(fileOut);
fileOut.close();
}
catch (Exception e) {
System.out.print(e.getMessage());
}
}
}


Compile and execute

Save above program in FontStylesExample.java file, and compile and execute as shown below

$ javac FontStylesExample.java
$ java FontStylesExample


Above will output something like this

Font Style example with Apache POI

 

Like wise we can modify the above program to add text direction to our text too. The code example is shown below 

package com.sc;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

/**
 * Created by SubjectCoach.com on 3/12/2015.
 * POI version 3.11
 */
public class FontRotationExample {
    public static void main(String[] args) {
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("FontStyles");

        // Let's create our first row
        Row row = sheet.createRow(1);

        // Create a new font and alter it.
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short)19);
        font.setFontName("Courier");
        // make our font italic
        font.setItalic(true);
        // how about a line through
        font.setStrikeout(true);
        // color red
        font.setColor((short) 2);
        // Fonts are set into a style so create a new one to use.
        CellStyle style = wb.createCellStyle();
        // apply font to cell style
        style.setFont(font);

        // set rotation of our text to 65 degrees
        style.setRotation((short) 65);

        // Create a cell and put a value in it.
        Cell cell = row.createCell(1);
        cell.setCellValue("Font Style testing");
        cell.setCellStyle(style);

        try {
            // Write the output to a file
            FileOutputStream fileOut = new FileOutputStream("c:/poi/fontrotationexample.xlsx");
            wb.write(fileOut);
            fileOut.close();
        }
        catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
}

Compile and execute

Save above program in FontRotationExample.java file, Compile and run it as shown below

$ javac FontRotationExample .java
$ java FontRotationExample 


Above will output something like this

 Font Rotation example with Apache POI

 

Description

This tutorial covers Apache POI, This tutorial is divided into 12 parts as listed below

  • What is Apache POI
  • Environment
  • Core Classes
  • Workbooks
  • Spreadsheets
  • Cells
  • Fonts
  • Formula
  • Hyperlink and defiining Print Area
  • Database 

Let us know if we made any error, your feedback is important. 



Prerequisites

Its is important that you have working knowledge of Java Programming language

Audience

Beginners seeking a quick introduction to Apache POI

Learning Objectives

To get you started with Apache POI

Author: Subject Coach
Added on: 10th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!