Auto Fit Rows and Columns in Excel in Java

MS Excel provides a feature to autofit the size of the cells by the length of their content. You can enable autofit for a row’s height and a column’s width. While generating and processing Excel spreadsheets programmatically, you may need to enable this feature dynamically. To achieve that, this article shows how to autofit rows and columns in Excel XLS in Java. We will explicitly cover autofit for a row’s height and a column’s width with code samples.

Java API to AutoFit Excel Rows and Columns - Free Download

Aspose.Cells for Java is an amazing API with a rich set of features for creating and processing spreadsheets. You can implement basic as well as advanced features for generating complex Excel files seamlessly. We are going to use this API to autofit rows and columns in Excel files. You can either download its JAR or install it directly using the following Maven configurations.

Repository:

<repository>
	<id>AsposeJavaAPI</id>
	<name>Aspose Java API</name>
	<url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-cells</artifactId>
	<version>22.8</version>
</dependency>

AutoFit Rows in Excel in Java

To autofit rows in an Excel XLS/XLSX, you first need to access the desired worksheet and then enable autofit for the rows using their index. The following are the steps to autofit Excel rows in Java.

The following code sample shows how to autofit a row in Excel in Java.

How to AutoFit Excel Columns in Java

The autofit for columns requires you to perform the same steps as in the previous section. The only difference is of the method that you will call to autofit a column’s width. The following are the steps to autofit a column in Excel in Java.

The following code sample shows how to autofit a column in Excel in Java.

Java API to AutoFit Excel Rows and Columns - Get a Free License

You can get a free temporary license to autofit rows and columns in your Excel files without evaluation limitations.

Conclusion

In this article, you have learned how to autofit rows and columns in Excel files in Java. The code samples have demonstrated how to enable autofit for a specific row or column using its index programmatically.

Explore Aspose’ Spreadsheet API for Java

You can visit the documentation to explore other features of Aspose.Cells for Java. Also, you can share your questions or queries with us via our forum.

See Also