Documents for Excel, Java Edition Documentation
Features / Worksheet / Range Operations / Auto Fit Row Height and Column Width
In This Topic
    Auto Fit Row Height and Column Width
    In This Topic

    GcExcel Java 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 Java 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 Java:

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

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
            
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
            
    // Auto fit column width of range 'A1'
    worksheet.getRange("A1").setValue("Grapecity Documents for Excel");
    worksheet.getRange("A1").getColumns().autoFit();
    
    // Auto fit row height of range 'B2'
    worksheet.getRange("B2").setValue("Grapecity Documents for Excel");
    worksheet.getRange("B2").getFont().setSize(20);
    worksheet.getRange("B2").getRows().autoFit();
    
    // Auto fit column width and row height of range 'C3'
    worksheet.getRange("C3").setValue("Grapecity Documents for Excel");
    worksheet.getRange("C3").getFont().setSize(32);
    worksheet.getRange("C3").autoFit();
    
    // Saving the workbook to xlsx
    workbook.save("AutoFitRowHeightColumnWidth.xlsx");

    Note: The Auto fit feature has the following limitations :

    1) In a merged cell, the autoFit methods will not be applied. 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 the these methods too frequently.