Java: Change Row Height and Column Width in Excel

Adjusting row heights and column widths allows users to optimize the display of their data in a spreadsheet. Whether you're working with a large dataset or preparing a report, customizing these dimensions can help ensure that your information is presented clearly and concisely. Excel provides several ways to change row height and column width, including manual adjustments and automatic fitting options.

In this article, you will learn how to programmatically change row height and column width in Excel in Java using the Spire.XLS for Java library.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>14.11.0</version>
    </dependency>
</dependencies>
    

Change Row Height and Column Width for a Specific Row and Column

Spire.XLS for Java provides the Worksheet.setRowHeight() and Worksheet.setColumnWidth() methods for adjusting the height of a specific row and the width of a specific column in a worksheet. Here are the detailed steps to accomplish this task.

  • Create a Workbook object.
  • Load an Excel document from a given file path.
  • Get a specific worksheet from the workbook.
  • Change the height of a specific row using Worksheet.setRowHeight() method.
  • Change the width of a specific column using Worksheet.setColumnWidth() method.
  • Save the workbook to a different Excel file.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class SetRowHeightAndColumnWidth {

    public static void main(String[] args) {

        // Create a Workbook object
        Workbook workbook = new Workbook();

        // Load an Excel document
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx");

        // Get a specific worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        // Set the height of a selected row to 20
        worksheet.setRowHeight(1,20);

        // Set the width of a selected column to 30
        worksheet.setColumnWidth(4, 30);

        //Save to file.
        workbook.saveToFile("output/SetHeightAndWidth.xlsx", ExcelVersion.Version2016);
        
        // Dispose resources
        workbook.dispose();
    }
}

Change Row Height and Column Width for All Rows and Columns

To modify the row height and column width for all rows and columns in a worksheet, you can utilize the Worksheet.setDefaultRowHeight() and Worksheet.setDefaultColumnWidth() methods. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document from a given file path.
  • Get a specific worksheet from the workbook.
  • Change the height for all rows using Worksheet.setDefaultRowHeight() method.
  • Change the width for all columns using Worksheet.setDefaultColumnWidth() method.
  • Save the workbook to a different Excel file.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class SetRowHeightColumnWidthForAll {

    public static void main(String[] args) {

        // Create a Workbook object
        Workbook workbook = new Workbook();

        // Load an Excel document
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx");

        // Get a specific worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        // Set the default row height to 18
        worksheet.setDefaultRowHeight(18);

        // Set the default column width to 15
        worksheet.setDefaultColumnWidth(15);
        
        //Save to file.
        workbook.saveToFile("output/SetHeightAndWidthForAll.xlsx", ExcelVersion.Version2016);

        // Dispose resources
        workbook.dispose();
    }
}

Automatically Adjust Row Height and Column Width for a Specific Row and Column

To automatically adjust the row height and column width to fit the content of a specific row and column in a worksheet, you can use the Worksheet.autoFitRow() and Worksheet.autoFitColumn() methods. The steps to autofit row height and column width are as follows.

  • Create a Workbook object.
  • Load an Excel document from a given file path.
  • Get a specific worksheet from the workbook.
  • Automatically adjust the height of a specific row using Worksheet.autoFitRow() method.
  • Automatically adjust the width of a specific column using Worksheet.autoFitColumn() method.
  • Save the workbook to a different Excel file.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class AutoFitRowHeightAndColumnWidth {

    public static void main(String[] args) {

        // Create a Workbook object
        Workbook workbook = new Workbook();

        // Load an Excel document
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx");

        // Get a specific worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        // Autofit the first row 
        worksheet.autoFitRow(1);

        // Autofit the second column
        worksheet.autoFitColumn(2);

        // Save the document
        workbook.saveToFile("output/AutoFit.xlsx", ExcelVersion.Version2016);

        // Dispose resources
        workbook.dispose();
    }
}

Automatically Adjust Row Height and Column Width in a Cell Range

To automatically adjust the row height and column width within a specific cell range in your worksheet, you can utilize the CellRange.autoFitRows() and CellRange.autoFitColumns() methods respectively. Below are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document from a given file path.
  • Get a specific worksheet from the workbook.
  • Get a cell range using Worksheet.getCellRange() method.
  • Automatically adjust the row height in the range using CellRange.autoFitRow() method.
  • Automatically adjust the column width in the range using CellRange.autoFitColumn() method.
  • Save the workbook to a different Excel file.
  • Java
import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class AutoFitInRange {

    public static void main(String[] args) {

        // Create a Workbook object
        Workbook workbook = new Workbook();

        // Load an Excel document
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx");

        // Get a specific worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        // Get the used range
        CellRange cellRange = worksheet.getAllocatedRange();

        // Or, you can get a desired cell range
        // CellRange cellRange = worksheet.getCellRange(1,1,6,4)

        // Autofit rows and columns in the range
        cellRange.autoFitRows();
        cellRange.autoFitColumns();

        // Save the document
        workbook.saveToFile("output/AutoFit.xlsx", ExcelVersion.Version2016);

        // Dispose resources
        workbook.dispose();
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.