Adjust Row Height and Column Width in Excel using Java

In MS Excel, you can adjust the height of the rows and the width of the columns according to the content. For this, MS Excel provides Cell Width and Row Height options in the Format menu of the Cells group. But how would you do it while working with Excel files programmatically in Java? So let’s have a look at how to adjust row height and column width in Excel in Java.

Java API to Adjust Excel Row Height and Column Width

Aspose.Cells for Java is a popular API to work with spreadsheet documents. Using the API, you can create, process, and manipulate Excel files seamlessly. We will use this API to set the height of rows and width of columns in Excel files. You can either download the API or install it 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>

Adjust Row’s Height in Excel in Java

The following are the steps to adjust a row’s height in an Excel file in Java.

  • First, use Workbook class to load the Excel file.
  • Then, use Workbook.getWorksheets().get(index) method to access the desired worksheet by index.
  • Get cells collection in an object using Worksheet.getCells() method.
  • Call setRowHeight() method and pass rows’s index and height as parameters.
  • Finally, save the updated Excel file using Workbook.save(String) method.

The following code sample shows how to set the row’s height in Excel in Java.

The following screenshot shows the Excel worksheet after setting the row’s height.

Adjust Row Height in Excel using Java

Set Column’s Width in Excel in Java

The following are the steps to set a column’s width in an Excel file in Java.

  • Load the Excel file using the Workbook class.
  • Use Workbook.getWorksheets().get(index) method to access the desired worksheet by index.
  • Get cells collection using Worksheet.getCells() method.
  • Call setColumnWidth() method and pass column’s index and width as parameters.
  • Finally, save the updated Excel file using Workbook.save(String) method.

The following code sample shows how to set a column’s width in an Excel file in Java.

The following is the screenshot of the Excel worksheet after adjusting the column’s width.

Adjust Column Width in Excel using Java

Java API to Set Excel Row Height and Column Width - Get a Free License

You can get a free temporary license to set the row height and column width in Excel files without evaluation limitations.

Java Excel API - Explore More

You can explore more about the Java Excel API using the documentation. Also, you can have a look at the API references here.

Conclusion

In this article, you have learned how to adjust rows’ height and columns’ width in Excel using Java. We have demonstrated how to set a particular height for a row and width for a column with the help of code samples and screenshots.

Have a Question?

In case you would have any questions or queries, you can share them with us via our forum.

See Also