reading excel using scriptom for groovy, producing xml
Posted
by john
on Stack Overflow
See other posts from Stack Overflow
or by john
Published on 2010-05-12T08:18:47Z
Indexed on
2010/05/12
8:24 UTC
Read the original article
Hit count: 365
Dear friends,
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 some thing missing
could some experts enlighten me about what might go wrong?
I copied the program and result below.
thanks!
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/
© Stack Overflow or respective owner