Autofit Rows and Columns

Auto Fitting

Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file.

A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods for managing a worksheet. This article looks at using the Worksheet class to autofit rows or columns.

AutoFit Row - Simple

The most straight-forward approach to auto-sizing the width and height of a row is to call the Worksheet class' autoFitRow method. The autoFitRow method takes a row index (of the row to be resized) as a parameter.

AutoFit Row in a Range of Cells

A row is composed of many columns. Aspose.Cells allows developers to auto-fit a row based on the content in a range of cells within the row by calling an overloaded version of the autoFitRow method. It takes the following parameters:

  • Row index, the index of the row about to be auto-fitted.
  • First column index, the index of the row’s first column.
  • Last column index, the index of the row’s last column.

The autoFitRow method checks the contents of all the columns in the row and then auto-fits the row.

AutoFit Column - Simple

The easiest way to auto-size the width and height of a column is to call the Worksheet class' autoFitColumn method. The autoFitColumn method takes the column index (of the column about to be resized) as a parameter.

AutoFit Column in a Range of Cells

A column is composed of many rows. It is possible to auto-fit a column based on the content in a range of cells in the column by calling an overloaded version of autoFitColumn method that takes the following parameters:

  • Column index, represents the index of the column whose contents need to auto-fit
  • First row index, represents the index of the first row of the column
  • Last row index, represents the index of the last row of the column

The autoFitColumn method checks the contents of all rows in the column and then auto-fits the column.

AutoFit Rows for Merged Cells

With Aspose.Cells it is possible to autofit rows even for cells that have been merged using the AutoFitterOptions API. AutoFitterOptions class provides AutoFitMergedCellsType property that can be used to autofit rows for merged cells. AutoFitMergedCellsType accepts AutoFitMergedCellsType enumerable which has the following members.

  • NONE: Ignore merged cells.
  • FIRST_LINE: Only expands the height of the first row.
  • LAST_LINE: Only expands the height of the last row.
  • EACH_LINE: Only expands the height of each row.

You may also use the overloaded versions of autoFitRows & autoFitColumns methods accepting a range of rows/columns and an instance of AutoFitterOptions to auto-fit the selected rows/columns with the desired AutoFitterOptions accordingly.

The signatures of aforesaid methods are as follow:

  1. autoFitRows(int startRow, int endRow, AutoFitterOptions options)
  2. autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)

Important to Know