I am trying to export data from my jsp to excel using Apache POI. The Number of columns that I need will depend on two parameters and hence, I fetch it through a query.
For EX : If No. of dynamic columns required = 3, then output should be something like this.
A | B | DH1 | DH2 | DH3 | C
Here A, B, C = Static column Header.
DH = Dynamic column Header ( fetched from the query.)
To do this , I initially set the A, B,C , then find the No. of DH columns required. then I generate the DH columns. After that, I find out the value needed in each DH column , store that value in an Arraylist, and then traverse Arraylist and set the value.
Here the code
HSSFCell c1= row.createCell(0);
HSSFCell c2= row.createCell(1);
HSSFCell cellObject = null;
oRecordMetaInfo = getNoofDynamicColumns();
NoofColumns = oRecordMetaInfo.getRecordCount();
for(int i = 2; i < NoofColumns + 2; i++)
{
cellObject = row.createCell((int) i);
}
HSSFCell c3 = row.createCell((int) (NoofColumns + 2));
c1.setCellValue(" A ");
c2.setCellValue(" B ");
Array1 = new ArrayList();
Array1 = dao.getDynamicColumnValues();
If(Array1.size != 0)
{
QueryRow oRow = null;
Iterator oIt = null;
oIt = Array1.iterator();
while (oIt.hasNext()) {
oRow = (QueryRow) oIt.next();
oRow.get("DYNAMIC_VALUE");
cellObject.setCellValue(oRow.get("DYNAMIC_VALUE").getString());
}
}
c3.setCellValue(" C ");
But , the outtpu that I get is
A | B | | | DH3 | C
Hence only the last value gets set in the cell, the previous value are not. Can anyone suggest what's wrong ?