0
votes

I'm using POI to generate the Excel spreadsheet from my managed bean. Here's my Student Bean:

package edu.phc.students.model;

public class Student {
private String m_firstName; 
private String m_lastName; 
private String m_id;

public Student() {} 

public Student(
String firstName, 
String lastName, 
String id,
) 
{ 
        m_firstName=firstName; 
        m_lastName=lastName;   
        m_id=id;

} 

public String getid() { 
        return m_id; 
} 

public void setid(String newid) { 
        m_id=newid; 
} 

public String getFirstname() { 
        return m_firstName; 
} 

public void setFirstname(String newFirstName) { 
        m_firstName=newFirstName; 
} 

public String getLastname() { 
        return m_lastName; 
} 

public void setLastname(String newLastname) { 
        m_lastName=newLastname; 
}    

}

This is a small segment of the final code (SSJS).

var students:java.util.List = studentlist.getStudents();
var iterator:java.util.Iterator = students.iterator();
var count = 0;
while (iterator.hasNext()) {
        var student:edu.phc.students.model.Student = iterator.next();
        count++;
        var row:HSSFRow = sheet1.createRow(count);
        for( f = 0 ; f <= fieldList.length-1 ; f++){//column List
            // output student's data into Excel row 
            row.createCell((java.lang.Integer)(f)).setCellValue(student.getid());               
        }
}

This works perfectly. It generates the spreadsheet with the column names and inserts the student ID into each cell (for each row returned by the iterator).

However, I want to iterate through the properties available for the student bean - ID, Lastname, Firstname etc. and populate the cells of the spreadsheet. How do I do that? Can I reference the properties of the student bean by index?

Thanks,

Dan

1

1 Answers

1
votes

You could use java.lang.reflection. Here is an example for all fields in your student class:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

   <xp:repeat id="repeat1" rows="30" var="field">
      <xp:this.value><![CDATA[#{javascript: 
         var student = new edu.phc.students.model.Student();
         student.getClass().getDeclaredFields()}]]>
      </xp:this.value>
      <xp:label value="#{field.name}" id="labelFieldName" /><xp:br />
   </xp:repeat>
</xp:view>

The output looks like this:

m_firstName
m_lastName
m_id

Please check the documentation for other options.

EDIT:

This example iterates the methods and returns the current values of all methods which return a String:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

<xp:repeat id="repeat1" rows="30" var="method">
    <xp:this.value><![CDATA[#{javascript: 
        var student = new edu.phc.students.model.Student();
        student.setid("A");
        student.setLastname("Meiser");
        student.setFirstname("Hans");
        student.getClass().getDeclaredMethods()}]]>
    </xp:this.value>
    <xp:label id="label1">
        <xp:this.value>
            <![CDATA[#{javascript:
                if( method.getReturnType().getSimpleName()  == 'String' )
                    return method.invoke(student, null );}]]>
        </xp:this.value>
    </xp:label>
    <xp:br />
</xp:repeat>

</xp:view>

This will create the following output:

A

Hans

Meiser