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.

try
{
    final DDEClientConversation conversation = new DDEClientConversation();

    conversation.connect("Excel", "Sheet1");
    try
    {
        // 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!");
        // Sending "close()" command
        conversation.execute("[close()]");
    }
    finally
    {
        conversation.disconnect();
    }
}
catch (DDEMLException e)
{
    System.out.println("DDEMLException: 0x" + Integer.toHexString(e.getErrorCode())
                       + " " + e.getMessage());
}
catch (DDEException e)
{
    System.out.println("DDEException: " + e.getMessage());
}

Download ExcelExample.java

Subscribe to Excel cells modification.

The following example listens to changes in Excel cells. The setEventListener method is used to set event handler which will print A1 cell changes to console of Java application.

Example application will connect to running Excel, so, please start Microsoft Excel before trying example.

try
{
    final DDEClientConversation conversation = new DDEClientConversation();

    conversation.setEventListener(new DDEClientEventListener()
    {
        public void onDisconnect()
        {
            System.out.println("onDisconnect()");
        }

        public void onItemChanged(String topic, String item, String data)
        {
            System.out.println("onItemChanged("+topic+","+item+","+data.trim()+")");
        }
    });

    conversation.connect("Excel", "Sheet1");
    try
    {
        conversation.startAdvice("R1C1");

        System.out.println("Press Enter to quit");
        System.in.read();

        conversation.stopAdvice("R1C1");
    }
    finally
    {
        conversation.disconnect();
    }
}
catch (DDEMLException e)
{
    System.out.println("DDEMLException: 0x" + Integer.toHexString(e.getErrorCode())
                       + " " + e.getMessage());
}
catch (DDEException e)
{
    System.out.println("DDEException: " + e.getMessage());
}

Download ExcelAdviceExample.java

How to avoid java.lang.UnsatisfiedLinkError

Exception in thread "main" java.lang.UnsatisfiedLinkError: no JavaDDE in java.library.path
 at java.lang.ClassLoader.loadLibrary(Unknown Source)
 at java.lang.Runtime.loadLibrary0(Unknown Source)
 at java.lang.System.loadLibrary(Unknown Source)
 at com.pretty_tools.dde.client.DDEClientConversation.(DDEClientConversation.java:384)
 at ExcelExample.main(ExcelExample.java:21)

JDDE is JNI based library and it requires native code library (DLL for Windows). JavaDDE.dll should be placed to current directory from which you run your example, or you should specify JVM java.library.path parameter that points for folder with JavaDDE.dll.
Example:

 java -Djava.library.path="C:\jdde" ExcelExample

Other examples

Java DDE Server Example
DDE Request Example
DDE Poke Example
DDE Execute Example
DDE Advice Example