Set Row Height and Column Width in Excel using a free Java API

Andrew Wilson
1 min readFeb 25, 2021

In most cases, we need to adjust the row height and column width to better display the contents of certain Excel cells. This article will introduce how to set the row height and column width programmatically by using a free Java API (Free Spire.XLS for Java).

Installation (2 Method)
1# Download the free API and unzip it, then add the Spire.Xls.jar file to your project as dependency.

2# You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>

Relevant code snippet

import com.spire.xls.*;

public class SetWidthAndHeight {
public static void main(String[] args) {

//Load the sample document
Workbook workbook = new Workbook();
workbook.loadFromFile("sample.xlsx");

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

//Set the height of the third row to 25
worksheet.setRowHeight(3,25);

//Set the width of the second column to 20
worksheet.setColumnWidth(2, 20);

//Save to file
workbook.saveToFile("output/SetWidthAndHeight.xlsx", ExcelVersion.Version2016);
}
}

Output

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.