Multidimensional array as parameter

Frank Bos <[email protected]> Wed, 4 Jan 2006 10:48:32 -0500
Newsgroups gmane.comp.windows.devel.jawin
Message-ID <LISTSERV%[email protected]>
Hi,

I'm trying to use Jawin to fill an Excel sheet using a Java application.
For my application it is important to use "bulk" methods to set the values
for all cells in a range, because otherwise it will get too slow.

According to the Excel api, it should be possible to use a two-dimensional
array to set the "Value" property of a "Range" object. Unfortunately I
haven't been able to get this to work using Jawin. If I use a (one
dimensional) array, then it is possible to set a complete row of values
using a single Jawin call, but that is not good enough because my
application will output many rows to Excel (and not so many columns).

Example code:

public class JawinTest {
  public static void main(String args[]) {
    try {
      Ole32.CoInitialize();

      DispatchPtr app = new DispatchPtr("Excel.Application");
      app.put("Visible", true);
      DispatchPtr books = (DispatchPtr)app.get("Workbooks");
      DispatchPtr book = (DispatchPtr) books.invoke("Add");
      DispatchPtr sheets = (DispatchPtr)book.get("Worksheets");
      DispatchPtr sheet = (DispatchPtr)sheets.get("Item", new Integer(1));

      // Create a range which contains 3 cells
      DispatchPtr range1 = (DispatchPtr)sheet.get("Range", "A1:A3");

      // This will fill cells A1, A2 and A3 with "TestValue"
      range1.put("Value", "TestValue");

      // The three cells get value "TestValue1".
      // The other values get ignored!
      DispatchPtr range2 = (DispatchPtr)sheet.get("Range", "B1:B3");
      range2.put("Value2", new String[]
         {"TestValue1", "TestValue2", "TestValue3"});

      // Try a 2-dimensional array instead. Cells remain empty.
      DispatchPtr range3 = (DispatchPtr)sheet.get("Range", "C1:C3");
      range3.put("Value", new String[][] {
        {"TestValue1"},
        {"TestValue2"},
        {"TestValue3"}});

      // Maybe this way? Nope, doesn't work either.
      range3.put("Value", new String[][] {
 {"TestValue1", "TestValue2", "TestValue3"}});

      // Try filling a row instead. That works!
      // The three cells get different values.
      DispatchPtr range4 = (DispatchPtr)sheet.get("Range", "D1:F1");
      range4.put("Value", new String[]
        {"TestValue1", "TestValue2", "TestValue3"});

      Ole32.CoUninitialize();
    } catch (COMException exc) {
      exc.printStackTrace();
    }
  }
}

Am I doing something wrong or does Jawin have no support for 2d arrays? It
would be great if anyone can help me with this!

-- Frank