views:

202

answers:

0

I got a program from http://kousenit.wordpress.com/2007/03/27/groovyness-with-excel-and-xml.

But I got some very strange results:

  1. I can still print XML, but two records are not readable.
  2. I got exception suggesting something is missing

What might go wrong?

I copied the program and result below.

import org.codehaus.groovy.scriptom.ActiveXObject
def addresses = new File('addresses1.xls').canonicalPath
def xls = new  ActiveXObject('Excel.Application')

def workbooks = xls.Workbooks
def workbook = workbooks.Open(addresses)

// select the active sheet
def sheet = workbook.ActiveSheet
sheet.Visible = true


// get the XML builder ready
def builder = new groovy.xml.MarkupBuilder()
builder.people {

    for (row in 2..1000) {
        def ID = sheet.Range("A${row}").Value.value
        if (!ID) break

        // use the builder to write out each person
        person (id: ID) {
            name {
                firstName sheet.Range("B${row}").Value.value
                lastName sheet.Range("C${row}").Value.value
            }

            address {
                street sheet.Range("D${row}").Value.value
                city sheet.Range("E${row}").Value.value
                state sheet.Range("F${row}").Value.value
                zip sheet.Range("G${row}").Value.value
            }
        }
    }
}

// Close the workbook without asking for saving the file.
workbook.Close(false, null, false)
// quits excel
xls.Quit()
xls.release()

However, I got the following results:

<people>
  <person id='1234.0'>
    <name>
      <firstName>[C@128a25</firstName>
      <lastName>[C@5e45</lastName>
    </name>
    <address>
      <street>[C@179ef7c</street>
      <city>[C@12f95de</city>
      <state>[C@138b554</state>
      <zip>12345.0</zip>
    </address>
  </person>
</person>

Exception thrown
May 12, 2010 4:07:15 AM org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

java.lang.NullPointerException

    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoFieldSite.acceptGetProperty(GetEffectivePojoFieldSite.java:43)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:237)

    at sriptom4_excel$_run_closure1.doCall(sriptom4_excel.groovy:18)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


  [1]: http://kousenit.wordpress.com/2007/03/27/groovyness-with-excel-and-xml/