Skip to content
Related Articles

Related Articles

How to Set Direction to the Text in Cell using Java?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 04 Dec, 2020
View Discussion
Improve Article
Save Article

Apache POI is an open-source library by Apache which can be used to create, modify and display files MS office file in Java. It provides classes and methods to do so. This API provides various components such as POIFS (Poor Obfuscation Implementation File System), HSSF (Horrible Spreadsheet Format), XSSF (XML Spreadsheet Format), HPSF (Horrible Property Set Format), HWPF (Horrible Word Processor Format), XWPF (XML Word Processor Format), HSLF (Horrible Slide Layout Format). Data in a spreadsheet is always stored in a cell. Apache library allows you to perform various operations on these cells such as changing font style, add a hyperlink, changes text direction.

Apache library allows you to change the direction of the text in a cell by displaying the text in different angles of directions. So we specify an angle in the function. E.g. setRotation((short) 20);

For this example, XSSF components are used.

  1. First, download the library, It will download all the required jar files.
  2. Right Click on a project in package explorer -> click on Build path -> Configure Build Path -> Add libraries -> Add external  Jar files -> select the JAR files from the unzipped folder.

Following are the classes that will be used in the code:

  • XSSFWorkbook: Root class to handle XLSX. It reads an excel file from a file input stream.
  • XSSFSheet: A workbook can have more than one sheet.
  • XSSFRow: Used to represents a row of excel sheet.
  • XSSFCell: Used to represents a cell to the corresponding row.


// Java program to Set the Direction of Cell
// packages from POI library
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class workbook {
    public static void main(String[] args) throws Exception
        // creating workbook
        XSSFWorkbook wb = new XSSFWorkbook();
        // creating spreadsheet named 'sheet1'
        XSSFSheet sheet = wb.createSheet("sheet1");
        // creating first row
        XSSFRow r1 = sheet.createRow(0);
        // creating cells for first row
        // first cell
        XSSFCell r1c1 = r1.createCell(0);
        // second cell
        XSSFCell r1c2 = r1.createCell(1);
        // create object of XSSFCellStyle
        // XSSFCellStyle is used for formatting and applying
        // different styles such as rotation, alignment
        XSSFCellStyle style = wb.createCellStyle();
        XSSFCellStyle style1 = wb.createCellStyle();
        // setting rotation angle for object style, style1
        // creating first row
        XSSFRow r2 = sheet.createRow(1);
        // creating cells for second row
        // first cell
        XSSFCell r2c1 = r2.createCell(0);
        // applying the object style
        // second cell
        XSSFCell r2c2 = r2.createCell(1);
        FileOutputStream out = new FileOutputStream(
            new File("D:/sheet.xlsx"));

Output in Excel sheet:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!