Search in sources :

Example 1 with ComExcel_Application

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

the class Excelautomation_KB_219151_Mod method main.

public static void main(String[] args) throws IOException {
    // Initialize COM Subsystem
    Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
    // Initialize Factory for COM object creation
    Factory fact = new Factory();
    // Set LCID for calls to english locale. Without this formulas need
    // to be specified in the users locale.
    fact.setLCID(new LCID(0x0409));
    try {
        // Start excel application
        ComExcel_Application excel = fact.createObject(ComExcel_Application.class);
        ComIApplication excelApp = excel.queryInterface(ComIApplication.class);
        // Set visiblite of application
        excelApp.setVisible(true);
        Helper.sleep(5);
        // Get a new workbook.
        ComIWorkbook wb = excelApp.getWorkbooks().Add();
        ComIWorksheet sheet = wb.getActiveSheet();
        // Add table headers going cell by cell.
        sheet.getCells().getItem(1, 1).setValue("First Name");
        sheet.getCells().getItem(1, 2).setValue("Last Name");
        sheet.getCells().getItem(1, 3).setValue("Full Name");
        sheet.getCells().getItem(1, 4).setValue("Salary");
        // Create an array to set multiple values at once.
        SAFEARRAY saNames = safeVariantArrayFromJava(new String[][] { { "John", "Smith" }, { "Tom", "Brown" }, { "Sue", "Thomas" }, { "Jane", "Jones" }, { "Adam", "Johnson" } });
        // Fill A2:B6 with an array of values (First and Last Names).
        VARIANT valueHolder = new VARIANT();
        valueHolder.setValue(Variant.VT_ARRAY | Variant.VT_VARIANT, saNames);
        sheet.getRange("A2", "B6").setValue(valueHolder);
        saNames.destroy();
        // Fill C2:C6 with a relative formula (=A2 & " " & B2).
        sheet.getRange("C2", "C6").setFormula("= A2 & \" \" & B2");
        // Fill D2:D6 with a formula(=RAND()*100000) and apply format.
        sheet.getRange("D2", "D6").setFormula("=RAND()*100000");
        sheet.getRange("D2", "D6").setNumberFormat("$0.00");
        // AutoFit columns A:D.
        sheet.getRange("A1", "D2").getEntireColumn().AutoFit();
        displayQuaterlySales(sheet);
        File tempFile = Helper.createNotExistingFile("exceloutput", ".xlsx");
        System.out.println("Writing output to: " + tempFile.getAbsolutePath());
        wb.SaveAs(tempFile.getAbsolutePath());
        excelApp.setUserControl(true);
    } finally {
        fact.disposeAll();
        Ole32.INSTANCE.CoUninitialize();
    }
    System.exit(0);
}
Also used : LCID(com.sun.jna.platform.win32.WinDef.LCID) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) ComIWorkbook(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook) Factory(com.sun.jna.platform.win32.COM.util.Factory) ComExcel_Application(com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication) ComIWorksheet(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)

Example 2 with ComExcel_Application

use of com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application 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

Factory (com.sun.jna.platform.win32.COM.util.Factory)2 ComExcel_Application (com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application)2 ComIApplication (com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication)2 ComIWorkbook (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook)2 ComIWorksheet (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)2 File (java.io.File)2 AbstractComEventCallbackListener (com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener)1 IComEventCallbackCookie (com.sun.jna.platform.win32.COM.util.IComEventCallbackCookie)1 ComIAppEvents (com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents)1 ComIRange (com.sun.jna.platform.win32.COM.util.office.excel.ComIRange)1 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)1 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)1 LCID (com.sun.jna.platform.win32.WinDef.LCID)1 IOException (java.io.IOException)1