Requesting data from Excel, change Excel cells, and executing commands.

The following example establishes a DDE connection with Microsoft Excel from Java application. Then it performs REQUEST operation to read data from A1 cell, POKE operation to change data in A1 cell, and EXECUTE operation to close the document.

Example application will connect to running Excel, so, please start Microsoft Excel before trying example, and enter any data to cell A1.

    final DDEClientConversation conversation = new DDEClientConversation();

    // Establish conversation with opened and active workbook
    conversation.connect("Excel", "Sheet1");
    // if you have several opened files, you can establish conversation using file path
    //conversation.connect("Excel", "C:\\Book1.xlsx");
    // or you can also specify Sheet2
    //conversation.connect("Excel", "C:\\[Book1.xlsx]Sheet2");
        // Requesting A1 value
        System.out.println("A1 value: " + conversation.request("R1C1"));
        // Changing cell A1 value to "We did it!"
        conversation.poke("R1C1", "We did it!");
        // Fill several cells of the same row, using \t as separator
        conversation.poke("R4", "Fill\tthe\trow");
        // Fill several cells of the same column, using \n as separator
        conversation.poke("C4", "Fill\nthe\ncolumn");
        // Fill several cells with matrix, using \t as column separator and \n as row separator
        conversation.poke("R5C5:R7C7", "1\t2\t3\n4\t5\t6\n7\t8\t9");
        // Run macro with name Macro1
        // Sending "close()" command
catch (DDEMLException e)
    System.out.println("DDEMLException: 0x" + Integer.toHexString(e.getErrorCode())
                       + " " + e.getMessage());
catch (DDEException e)
    System.out.println("DDEException: " + e.getMessage());

Download all examples and binaries

Command line to run example:

java -cp .;pretty-tools-JDDE-2.0.4.jar ExcelExample