Search in sources :

Example 1 with ComIAppEvents

use of com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents in project jna by java-native-access.

the class MSOfficeExcelDemo method testExcel.

public static void testExcel() throws IOException {
    File demoDocument = null;
    ComIApplication msExcel = null;
    Factory factory = new Factory();
    try {
        System.out.println("Files in temp dir: " + Helper.tempDir.getAbsolutePath());
        ComExcel_Application excelObject = factory.createObject(ComExcel_Application.class);
        msExcel = excelObject.queryInterface(ComIApplication.class);
        System.out.println("MSExcel version: " + msExcel.getVersion());
        msExcel.setVisible(true);
        Helper.sleep(5);
        demoDocument = Helper.createNotExistingFile("jnatest", ".xls");
        Helper.extractClasspathFileToReal("/com/sun/jna/platform/win32/COM/util/office/resources/jnatest.xls", demoDocument);
        ComIWorkbook workbook = msExcel.getWorkbooks().Open(demoDocument.getAbsolutePath());
        msExcel.getActiveSheet().getRange("A1").setValue("Hello from JNA!");
        // wait 1sec. before closing
        Helper.sleep(1);
        // Save document into temp and close
        File output = new File(Helper.tempDir, "jnatest.xls");
        output.delete();
        workbook.SaveAs(output.getAbsolutePath());
        msExcel.getActiveWorkbook().Close(false);
        //			// msExcel.newExcelBook();
        msExcel.getWorkbooks().Open(output.getAbsolutePath());
        msExcel.getActiveSheet().getRange("A2").setValue("Hello again from JNA!");
        class Listener extends AbstractComEventCallbackListener implements ComIAppEvents {

            volatile boolean SheetSelectionChange_called;

            @Override
            public void errorReceivingCallbackEvent(String message, Exception exception) {
            }

            @Override
            public void SheetSelectionChange(ComIWorksheet sheet, ComIRange target) {
                SheetSelectionChange_called = true;
            }
        }
        ;
        Listener listener = new Listener();
        IComEventCallbackCookie cookie = msExcel.advise(ComIAppEvents.class, listener);
        Helper.sleep(1);
        msExcel.getActiveSheet().getRange("A5").Activate();
        Helper.sleep(1);
        msExcel.unadvise(ComIAppEvents.class, cookie);
        System.out.println("Listener was fired: " + listener.SheetSelectionChange_called);
        // close and discard changes
        msExcel.getActiveWorkbook().Close(false);
    } finally {
        // Make sure the excel instance is shut down
        if (null != msExcel) {
            msExcel.Quit();
        }
        // Release all objects acquired by the factory
        factory.disposeAll();
        if (demoDocument != null && demoDocument.exists()) {
            demoDocument.delete();
        }
    }
}
Also used : ComIRange(com.sun.jna.platform.win32.COM.util.office.excel.ComIRange) AbstractComEventCallbackListener(com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener) ComIWorkbook(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook) AbstractComEventCallbackListener(com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener) Factory(com.sun.jna.platform.win32.COM.util.Factory) IOException(java.io.IOException) IComEventCallbackCookie(com.sun.jna.platform.win32.COM.util.IComEventCallbackCookie) ComIWorksheet(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet) ComExcel_Application(com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication) ComIAppEvents(com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents)

Aggregations

AbstractComEventCallbackListener (com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1 IComEventCallbackCookie (com.sun.jna.platform.win32.COM.util.IComEventCallbackCookie)1 ComExcel_Application (com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application)1 ComIAppEvents (com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents)1 ComIApplication (com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication)1 ComIRange (com.sun.jna.platform.win32.COM.util.office.excel.ComIRange)1 ComIWorkbook (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook)1 ComIWorksheet (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)1 File (java.io.File)1 IOException (java.io.IOException)1