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

    DsExcel 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, DsExcel 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 DsExcel 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("Documents for Excel");
    worksheet.getRange("A1").getColumns().autoFit();
    
    // Auto fit row height of range 'B2'
    worksheet.getRange("B2").setValue("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("Documents for Excel");
    worksheet.getRange("C3").getFont().setSize(32);
    worksheet.getRange("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. 

    Java
    Copy Code
    //create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getActiveSheet();
    worksheet.getRange("A1:D1").merge();
    worksheet.getPageSetup().setPrintGridlines(true);
    worksheet.getRange("A1:D1").setWrapText(true);
    worksheet.getRange("A1:D1").setValue("Automatically fit the row height of a merged cell in column-direction.");
    worksheet.getRange("A1:D1").getEntireRow().autoFit(true);
    //save to an pdf file
    workbook.save("testAutoFitWrapText.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.