Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java program to Set the Direction of Cell
import java.io.File;
import java.io.FileOutputStream;
  
// 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);
        r1c1.setCellValue("Name");
        // second cell
        XSSFCell r1c2 = r1.createCell(1);
        r1c2.setCellValue("Age");
  
        // 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
        style.setRotation((short)20);
        style1.setRotation((short)30);
  
        // creating first row
        XSSFRow r2 = sheet.createRow(1);
        
        // creating cells for second row
        // first cell
        XSSFCell r2c1 = r2.createCell(0);
        r2c1.setCellValue("GFG");
        
        // applying the object style
        r2c1.setCellStyle(style);
        
        // second cell
        XSSFCell r2c2 = r2.createCell(1);
        r2c2.setCellValue("10");
        r2c2.setCellStyle(style1);
  
        FileOutputStream out = new FileOutputStream(
            new File("D:/sheet.xlsx"));
        wb.write(out);
        out.close();
    }
}


Output in Excel sheet:



Last Updated : 04 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads