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.


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

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

//AutoFit column width and row height

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