How to Align Text in Excel Cells Using Java

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
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("C:\\Users\\Administrator\\Desktop\\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);
}
}

--

--

--

Sharing Java Code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SpecFlow Tips: Baseclass or Context Injection

Solr vs Elasticsearch: The Best open Search Search Engine?

Linux Kernel Testing Results by Linaro, Feb 28th edition

Tutorial Fuzzy Logic Mamdani for Arduino

Tutorial Fuzzy Logic Mamdani for Arduino

LeetCode Q#387 -First Unique Character in a String

Under The Hood: What Happens When You Type in aWebsite and Press Enter

Participating at Mozilla Festival-2017, London

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Wilson

Andrew Wilson

Sharing Java Code

More from Medium

How to deploy a Camunda Spring Boot application to an external application server

Ski boot debugging

Simple AWS lambda using java

Java I/O Byte Stream Implementation