[]
        
(Showing Draft Content)

Auto Fit Row Height and Column Width

GcExcel .NET provides support for automatic adjustment of row height and column width based on the data present in the rows and columns. The Auto Fit feature adjusts row height and column width so that every value in the rows or columns fits perfectly.

Advantage of Using Auto Fit Feature

When users need to work with spreadsheets containing huge amounts of data, some of the cells may contain values that appear cut off (if the cell width or height is too small) or contain extra spaces (if the cell width or height is too large). To avoid this anomaly and make the spreadsheets look much cleaner, GcExcel .NET enables users to automatically adjust the width of the columns and the height of the rows so as to auto fit the content inside the cell.

Further, the Auto fit feature is useful especially when you don't know how long every value is, how much space it will occupy and you also don't want to scroll through the entire spreadsheet to manually fix the row heights and column widths across the worksheet.

The following points should be kept in mind while working with the auto fit feature in GcExcel .NET:

  • This feature supports the auto adjustment of column width and row height of specific cell ranges only.

  • Users can use the AutoFit() method of the IRange interface in order to auto fit row height and column width.

  • If the type of the cell range used is a column (IRange.Columns/IRange.EntireColumn etc.), then only the column width will be adjusted to best fit but the row height would not be changed.

Refer to the following example code in order to automatically fit the row height and column width in a worksheet.

// Initialize workbook
Workbook workbook = new Workbook();
        
// Fetch the active worksheet 
IWorksheet worksheet = workbook.Worksheets[0];
        
// Auto fit column width of range 'A1'
worksheet.Range["A1"].Value = "Grapecity Documents for Excel";
worksheet.Range["A1"].Columns.AutoFit();

// Auto fit row height of range 'B2'
worksheet.Range["B2"].Value = "Grapecity";
worksheet.Range["B2"].Font.Size = 20;
worksheet.Range["B2"].Rows.AutoFit();

// Auto fit column width and row height of range 'C3'
worksheet.Range["C3"].Value = "Grapecity Documents for Excel";
worksheet.Range["C3"].Font.Size = 32;
worksheet.Range["C3"].AutoFit();

// Saving the workbook to xlsx
workbook.Save("AutoFitRowHeightColumnWidth.xlsx");

You can also use the overloaded AutoFit method which provides considerMergedCell parameter. The parameter, when set to true, allows you to automatically fit the row height of a merged cell (in column-direction). Please note that the merged cell should not contain more than one row. The following example code showcases such a scenario.

var workbook = new Workbook();
var sheet = workbook.ActiveSheet; sheet.Range["A1:D1"].Merge();
sheet.PageSetup.PrintGridlines = true;
sheet.Range["A1:D1"].WrapText = true;
sheet.Range["A1:D1"].Value = "Automatically fit the row height of a merged cell in column-direction.";
sheet.Range["A1:D1"].EntireRow.AutoFit(true);
workbook.Save("AutoFitMergeRow.xlsx");

The output of above code will look like below in Excel:


Note: The Auto fit feature has the following limitations :

  1. Apart from the above mentioned scenario, the AutoFit methods will not be applied in a merged cell. This behavior is same as in Excel.

  2. If the text in a cell is wrapped, the Auto fit for columns will not be applied to the cell.

  3. The AutoFit methods are time-consuming and impact the performance of the spreadsheet. In order to ensure the efficiency of spreadsheet applications, users should not call these methods too frequently.