Align Text in Excel Cells in Java

This article demonstrates how to align text in Excel cells using Spire.XLS for Java.

import com.spire.xls.*;

public class AlignText {
    public static void main(String[] args){
        //Create a workbook
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set the vertical alignment to Top
        sheet.getCellRange("B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Top);
        //Set the vertical alignment to Center
        sheet.getCellRange("B2").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
        //Set the vertical alignment to Bottom
        sheet.getCellRange("B3").getCellStyle().setVerticalAlignment(VerticalAlignType.Bottom);

        //Set the horizontal alignment to General
        sheet.getCellRange("B4").getCellStyle().setHorizontalAlignment(HorizontalAlignType.General);
        //Set the horizontal alignment to Left
        sheet.getCellRange("B5").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Left);
        //Set the horizontal alignment to Center
        sheet.getCellRange("B6").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
        //Set the horizontal alignment to Right
        sheet.getCellRange("B7").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Right);

        //Set the text orientation by using setRotation method
        sheet.getCellRange("B8").getCellStyle().setRotation(45);
        sheet.getCellRange("B9").getCellStyle().setRotation(90);

        //Set the text indentation
        sheet.getCellRange("B10").getCellStyle().setIndentLevel(6);

        //Set the text direction
        sheet.getCellRange("B11").getCellStyle().setReadingOrder(ReadingOrderType.LeftToRight);

        //Set the row height 
        sheet.getCellRange("B8").setRowHeight(60);
        sheet.getCellRange("B9").setRowHeight(60);

        //Save the result file
        workbook.saveToFile("AlignText.xlsx", ExcelVersion.Version2010);
    }
}

Output:

Align Text in Excel Cells in Java