AutoFit Column Width and Row Height in Excel in Java

Import Dependency (2 Method)

1# Download the free API (Free Spire.XLS for Java) and unzip it, then add the Spire.Xls.jar file to your project as dependency.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Sample Code

Free Spire.XLS for Java offers the CellRange.autoFitColumns() and CellRange.autoFitRows() methods for users to adjust the column width and row height in a range of cells. Below is the complete sample code.

import com.spire.xls.*;

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

//Load a sample Excel document
workbook.loadFromFile("E:\\Files\\Test1.xlsx");

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

//AutoFit column width and row height
worksheet.getAllocatedRange().autoFitColumns();
worksheet.getAllocatedRange().autoFitRows();

//Save the document
workbook.saveToFile("AutoFitCell.xlsx", ExcelVersion.Version2013);
}
}

--

--

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