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

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

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