Samples‎ > ‎Basics‎ > ‎

Hide Excel Ribbon and formula toolbar

import com.jniwrapper.Int32;
import com.jniwrapper.win32.automation.OleMessageLoop;
import com.jniwrapper.win32.automation.types.BStr;
import com.jniwrapper.win32.automation.types.VariantBool;
import com.jniwrapper.win32.com.types.LocaleID;
import com.jniwrapper.win32.excel.Window;
import com.jniwrapper.win32.excel._Application;
import com.jniwrapper.win32.jexcel.*;
import com.jniwrapper.win32.jexcel.ui.JWorkbook;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;

/**
 * This sample demonstrates how to hide ribbons and panels of integrated Excel workbook.
 */
public class HideExcelPanelsSample extends JFrame {
    private JWorkbook jWorkbook;


    public HideExcelPanelsSample() {
        super("Simple JWorkbook Window");

        jWorkbook = null;
        try {
            jWorkbook = new JWorkbook();
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());
            contentPane.add(jWorkbook, BorderLayout.CENTER);

            jWorkbook.getWorkbook().addWorkbookEventListener(new WorkbookEventAdapter() {
                @Override
                public void activated(WorkbookEventObject workbookEventObject) {
                    try {
                        hidePanels(jWorkbook);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void deactivated(WorkbookEventObject workbookEventObject) {
                    try {
                        restorePanels(jWorkbook);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            });

            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    if (jWorkbook != null) {
                        jWorkbook.close();
                    }
                }
            });

        } catch (ExcelException e) {
            throw new RuntimeException("Unable to create JWorkbook", e);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private static void hidePanels(JWorkbook workbook) throws InvocationTargetException, InterruptedException {
        Application application = workbook.getApplication();

        //Get the Application's native peer
        final _Application applicationPeer = application.getPeer();

        //All native peer operations must be executed from the OleMesssageLoop
        Runnable hideRibbonRunnable = new Runnable() {
            public void run() {
                Int32 locale = new Int32(LocaleID.LOCALE_USER_DEFAULT);
                VariantBool newState = new VariantBool(false);
                applicationPeer.setDisplayFormulaBar(locale, newState);
                Window activeWindow = applicationPeer.getActiveWindow();
                if (activeWindow != null && !activeWindow.isNull()) {
                    activeWindow.setDisplayWorkbookTabs(newState);
                    activeWindow.setAutoDelete(false);
                    activeWindow.release();
                }
                BStr hideRibbonMacro = new BStr("SHOW.TOOLBAR(\"Ribbon\",False)");
                applicationPeer.executeExcel4Macro(hideRibbonMacro, locale);
            }
        };

        //Get the Ole Message Loop object
        OleMessageLoop oleMessageLoop = application.getOleMessageLoop();
        //Run hide ribbon routine for native peer
        oleMessageLoop.doInvokeAndWait(hideRibbonRunnable);
    }

    private static void restorePanels(JWorkbook workbook) throws InvocationTargetException, InterruptedException {
        Application application = workbook.getApplication();

        //Get the Application's native peer
        final _Application applicationPeer = application.getPeer();

        //All native peer operations must be executed from the OleMesssageLoop
        Runnable hideRibbonRunnable = new Runnable() {
            public void run() {
                Int32 locale = new Int32(LocaleID.LOCALE_USER_DEFAULT);
                VariantBool newState = new VariantBool(true);
                applicationPeer.setDisplayFormulaBar(locale, newState);
                Window activeWindow = applicationPeer.getActiveWindow();
                if (activeWindow != null && !activeWindow.isNull()) {
                    activeWindow.setDisplayWorkbookTabs(newState);
                    activeWindow.setAutoDelete(false);
                    activeWindow.release();
                }
                BStr restoreRibbonMacro = new BStr("SHOW.TOOLBAR(\"Ribbon\",True)");
                applicationPeer.executeExcel4Macro(restoreRibbonMacro, locale);
            }
        };

        //Get the Ole Message Loop object
        OleMessageLoop oleMessageLoop = application.getOleMessageLoop();
        //Run hide ribbon routine for native peer
        oleMessageLoop.doInvokeAndWait(hideRibbonRunnable);
    }

    public static void main(String[] args) {
        HideExcelPanelsSample sampleWindow = new HideExcelPanelsSample();
        sampleWindow.setSize(800, 600);
        sampleWindow.setLocationRelativeTo(null);
        sampleWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
        sampleWindow.setVisible(true);
    }

}