Documents for Excel, Java Edition Documentation
Features / Workbook / Workbook Views
In This Topic
    Workbook Views
    In This Topic

    GcExcel Java enables users to customize the workbook appearance based on specific preferences.

    You can use the getBookView method of the IWorkbook interface in order to set the workbook display as per your choice.

    The IWorkbookView interface provides the following methods to allow users to further customize essential display settings in the workbook:

    Methods Descriptions

    getDisplayHorizontalScrollBar setDisplayHorizontalScrollBar

    Used to get or set the display of the horizontal scrollbar.            

    getDisplayVerticalScrollBar

    setDisplayVerticalScrollBar

                  

    Used to get or set the display of the vertical scrollbar.

    getDisplayWorkbookTabs

    setDisplayWorkbookTabs

                   

    Used to get or set the display of the workbook tabs.

    getTabRatio

    setTabRatio

    Used to get or set the ratio of the width of the tab area (of the workbook) to the width of the horizontal scroll bar (of the worksheet). The value of TabRatio can be any number between 0 and 1. By default, if the TabRatio is not set, the value is 0.6.

    In order to set the workbook view and customize other display settings, refer to the following code snippet.

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
            
    // Configure Workbook View Settings
    IWorkbookView bookView = workbook.getBookView();
    bookView.setDisplayVerticalScrollBar(false);
    bookView.setDisplayWorkbookTabs = true;
    bookView.setTabRatio(0.5);
            
    // Save to an excel file
    workbook.Save("ConfigureWorkbookView.xlsx");

    Custom Views

    GcExcel Java also lets you save the views created by customizing properties or methods as custom views. Collection of custom views stored in a workbook is represented by IWorkbook.getCustomViews method. Each custom view in the collection is represented by ICustomView object and stores the view state of all worksheets in the current workbook. You can set Name, RowColSettings and PrintSettings properties for each custom view. To add a new custom view to a workbook, you can use the add method. If a custom view name already exists in the collection, the new view overwrites the existing custom view. To apply a custom view to the workbook, you can use ICustomView.show method. Similarly, you can delete custom views from the collection by calling ICustomView.delete method.

    Note: The add method does not allow addition of custom view and throws an exception in following cases:

    • When workbook contains a table or pivot table
    • When name of the custom view is null or whitespace

     

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet worksheet1 = workbook.getActiveSheet();
    IWorksheet worksheet2 = workbook.getWorksheets().add();
            
    worksheet1.getRange("J12").setValue(1);
    worksheet2.getRange("J12").setValue(2);
    
    // Add the normal view.
    workbook.getCustomViews().add("Normal", true, true);
    
    worksheet1.getPageSetup().setPrintArea("$C$4:$J$12");
    worksheet2.getPageSetup().setPaperSize(PaperSize.A4);
    
    // Add a new view which saves the current page settings of all worksheets.
    workbook.getCustomViews().add("PrintSetting", true, false);
    
    worksheet1.getRange("5:6").setHidden(true);
    worksheet2.getRange("5:6").setHidden(true);
    
    // Add a new view which saves the current hidden rows/columns, filter settings of all worksheets.
    workbook.getCustomViews().add("HiddenRows", false, true);
    
    // Apply PrintSetting custom view 
    workbook.getCustomViews().get("PrintSetting").show();
            
    // In the exported file, the user can switch between custom views.
    workbook.save("CustomViewAdd.xlsx");